Basic C++ syntax

cout, endl

#include <iostream>
#include <string>
using namespace std;

int main()
{
	cout << "Please enter your name:";
	string input;
	
	cin >> input;

	cout << input << endl << "test" << endl;
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  • cin = read the value, can be string or int(if enter string to int, 0 will show)
  • cout = print out the result to the screen
  • endl = endline
  • << = send to output stream

output:
hello
test

Type of Variables

  • signed
  • unsigned
  • short
  • long

C++ Data Types

Functions

#include <iostream>
#include <string>
using namespace std;

void func1()
{
	cout << "test func1" << endl;
}

string func(string abc)
{
	cout << abc << endl;
	return "test return";
}

int main()
{
	func1();
	cout <<func("test") << endl;
	
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>
using namespace std;

void func1();

int main()
{
	func1();
	
	
	return 0;
}

void func1()
{
	cout << "test func1" << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  • We must declare the function if we want to put the function below the main(before it is called)

class

test.cpp

#include "stdafx.h"
#include <iostream>
#include "TestClass.h"

using namespace std;


int main()
{
	cout << "Starting ..." << endl;
	{                            // destory the class before closing
		TestClass test;
		test.fnTest();
		TestClass test1("a");    //new a class with parameter
		test1.fnTest();
	}
	cout << "Ending ..." << endl;
	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

testclass.h

#pragma once


using namespace std;

class TestClass
{
private:                   // private and public variable
	bool testbool;

public:
	TestClass();
	TestClass(string str);
	~TestClass();
	void fnTest();
	
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

testclass.cpp

#include "stdafx.h"
#include <iostream>

#include "TestClass.h"


using namespace std;


TestClass::TestClass()
{
	cout << "Class Created" << endl;
	testbool = false;
}

TestClass::TestClass(string str)  //contructor overloading
{
	if (str == "a")
		testbool = true;
	else
		testbool = false;
}


TestClass::~TestClass()  //destructor
{
	cout << "Class Destroyed" << endl;
}

void TestClass::fnTest()   //normal function
{
	if (testbool)
		cout << "Value is true" << endl;
	else
		cout << "Value is false" << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

Contructor initialization lists

better way to write constructor

test.cpp

#include "stdafx.h"
#include <iostream>
#include "TestClass.h"

using namespace std;


int main()
{
	cout << "Starting ..." << endl<< endl;
	{
		TestClass test;
		test.PrintPara();
		TestClass test2("test2a", "test2b");
		test2.PrintPara();
		TestClass test3("test3");
		test3.PrintPara();

	}
	cout << endl << "Ending ..." << endl;
	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

testclass.h

using namespace std;

class TestClass
{
private:
	string para1;
	string para2;

public:
	TestClass();        //method which write lots of code
	TestClass(string input1, string input2);      //method write less
	TestClass(string input1) :para1(input1), para2("auto") { cout << "class create" << endl; };   //just initialize the parameter in the header file
	~TestClass();
	void fnTest();
	void PrintPara();
	
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

testclass.cpp

#include "stdafx.h"
#include <iostream>
#include <string>
#include "TestClass.h"


using namespace std;

// set the the parameter one by one
TestClass::TestClass()
{
	cout << "Class Created" << endl;

	para1 = "undefined";
	para2 = "undefined";
}

// set the parameter in decoration
TestClass::TestClass(string input1, string input2): para1(input1), para2(input2){
	cout << "Class Created" << endl;
}

TestClass::~TestClass()
{
	cout << "Class Destroyed" << endl;
}

void TestClass::PrintPara()
{

	
		cout << "Para1: " << para1 << " Para2: " << para2 << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

Pointer

#include "stdafx.h"
#include <iostream>


using namespace std;

void ChangeValue(int* pointer)
{
	cout << "Before: " << *pointer << " Address:" << pointer << endl;
	*pointer = 10;
	cout << "After: " << *pointer << " Address:" << pointer << endl;
}

int main()
{

	int value = 8;
	int* pvalue = &value;
	cout << "Value address: " << &value << endl;
	cout << "Curent Value: " << value << " Value pointer pointing: "<< *pvalue << " Address:"<< pvalue << endl;

	ChangeValue(pvalue);

	cout << "Curent Value: " << value << " Value pointer pointing: " << *pvalue << " Address:" << pvalue << endl;
	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

output:
Value address: 0037FBA8
Curent Value: 8 Value pointer pointing: 8 Address:0037FBA8
Before: 8 Address:0037FBA8
After: 10 Address:0037FBA8
Curent Value: 10 Value pointer pointing: 10 Address:0037FBA8

Array pointers

The + and - symbol in Array pointer means move to next member of array instead of addition and substraction.

string texts[] = { "one", "two", "three" };

	string *pTexts = texts;

	cout << pTexts << " " << *(pTexts) << endl;
	cout << pTexts  + 1 << " " << *(pTexts + 1)<< endl;
	cout << pTexts + 2 << " " << *(pTexts + 2) << endl;


	for (int i = 0; i < sizeof(texts) / sizeof(string); i++) {
		cout << pTexts[i] << " " << flush;
	}

	cout << endl;

	for (int i = 0; i < sizeof(texts) / sizeof(string); i++, pTexts++) {
		cout << *pTexts << " " << flush;
	}

	cout << endl;

	string *pElement = &texts[0];
	string *pEnd = &texts[2];

	while (true) {
		cout << *pElement << " " << flush;

		if (pElement == pEnd) {
			break;
		}

		pElement++;
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Last Updated: 1/14/2019, 12:15:15 AM