C, C + +: references, pointers, instances, memory models, namespace

Source: Internet
Author: User

//HelloWorld.cpp:Defines the entry point for the console application.//#include"stdafx.h"#include"string.h"
#include "iostream.h"
/** * in C, C + + language * Declaration: Declare pointer variable with *, declare variable or constant is not added *. * For example, parameters in function declarations, return value types, and so on. For example, the Declaration of a field in a class. * In the assignment statement: * Indicates the value. & Represents the address of the variable, that is, the pointer is taken. * * Class,struct,unit: Also follow the above principles. In addition, * when using pointers for variable access, method invocation, to use * * In the use of the instance of the variable, the method of invocation, to be used. * For example Persontest () method: * p is a pointer, *p is the instance to which P is pointing. * * Persontest () memory model in test: * pointer P Value *p, also known as instance * ___________ __________ ___________ * | | | name |-------> | Fang Jinuo | *name * | | ————— > | age:23 | |___________| * |___________| | Address | ______________________ * |__________|-------> | Bei Jing, Haid Dian | *address * |_____________________| */classperson{Private: Char*name; intAge ; Char*address; Public : Char*toString () {Char* ret=name; returnret; } voidSetage (intAge ) { This->age=Age ; } voidSetaddress (Char*address) { This->address=address; } voidSetName (Char*name) { This->name=name; }};voidpersontest () { person* p=NewPerson (); P->setaddress ("Bei Jing, Hai Dian");//assigning values in the form of pointers(*p). SetName ("Fang Jinuo");//assigning values in the same way as objects(*p). Setage ( at); printf ("Show info:\n%s\n", (*p). toString ());}voidswitchtest () {intA= at; intb=1; intC=0; CharOper='+'; Switch(oper) { Case '+': C=a+b; Case '-': C=a-b; Break; Case '*': C=a*b; Break; } printf ("C =%d%c%d =%d", A, Oper, b, c);}/** * Input and output of C language*/voidInput_output_test_c () {printf ("Hello world!\n"); printf ("Zhang San, wo Cao \ n"); intb; printf ("input Tow int number, pattern is D, d:\n"); scanf ("%d,%d", &a, &b); printf ("A is%d\n", a); printf ("B is%d\n", B); printf ("a+b=%d\n", A +b);}

/**
* C + + input and output
* Include iostream.h required before use
*/
void Input_output_test_cpp ()

{

<< out1 << out2 << out3 << out4 << Endl;
Endl represents EndLine, that is, line break
char * content=new char;
cout << "Plese input:" << Endl;
Cin>> content;
cout << content << Endl;
}


/**
* Namespace
* C language is not supported.
* C + + support.
*
*/
Namespace MyNS
{
void Namespacetest () {
cout << "MyNS namespace invoked" << Endl;
}
}

void Namespacetest () {
cout << "Current namespace invoked" << Endl;
}

int Main (intChar* args[]) {

Input_output_test_c ();
Persontest ();
Switchtest ();
Input_output_test_cpp ();
Namespacetest ();
Myns::namespacetest ();

    return 0 ;}

C, C + +: references, pointers, instances, memory models, namespace

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.