C ++ syntax basic mode of operation

// c_plus_plus_test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;


//template
template <typename T>

T max(T a, T b)
{
	if (a > b)	return a;
	else return b;
}

//default parameter
int max_int(int a=1, int b=2)
{
	if (a > b) return a;
	else return b;
}

enum test_enum
{
	item0,
	item1,
	item2,
};


//class test

class student;	//提前声明

class student
{

private:
	int num;		//member variables
	string name;
	string sex;
	mutable int mutable_var;		//mutable variable

public:
	const static int static_var;			//static variable

public:
	void display();					//normal member function
	void const_display() const;		//const type member function
	void const_display2() const;

	bool operator > (student &stu) const;	// '>' operator reload
	operator int() const;		//type conversion function of 'int'

	friend void class_friend_function(student &a);	//friend function

	student(int num, string name="null", string sex="null");	//constructor reload and with default parameters
	student();		//constructor reload
	virtual ~student();		//destructor
};

void student::display() 	//member function
{
	mutable_var = 1;	//mutable variable can be accessed in any function

	cout << "display called" << endl;
	cout << "num:" << num << ",name:" << name << ",sex:" << sex << endl;
}

void student::const_display2() const
{
	cout << "const_display2" << endl;
}

void student::const_display() const
{
	mutable_var = 1;	//const function can only modify mutable variable
	const_display2();	//const function can only call const function

	cout << "const display called" << endl;
	cout << "num:" << num << ",name:" << name << ",sex:" << sex << endl;	//can refer to any member variables, dont change value
}

student::student(int num, string name, string sex)	//with default parameters
{
	this->num = num;
	this->name = name;
	this->sex = sex;

	cout << "reload constructor" << endl;
}

student::student()	//constructor reload
{
	num = 2;
	name = "wangming";
	sex = "male";

	cout << "default constructor" << endl;
}

student::~student()
{
	cout << "student destructor call" << endl;
}

//init static variable
const int student::static_var = 0x55;

//friend function
void class_friend_function(student &a)
{
	cout << "class student friend function test:" << a.num << endl;
}

bool student::operator>(student &stu) const
{
	if (this->num >= stu.num) return true;
	else return false;
}

student::operator int() const
{
	return num;
}


//derived class
class student_derived :public student
{
private:
	int derive_data;

public:
	void derive_function(void);
	void display(void);

	student_derived(int num=0, string name = "null", string sex = "null", int data = 0) :student(num, name, sex), derive_data(data){};
	~student_derived(){};
};

void student_derived::display(void)
{
	cout << "derived class function overload" << endl;
}
void student_derived::derive_function(void)
{
	cout << "in derive_function" << endl;
}

//anonymous namespace
namespace name1
{
	int ns_int = 0;
}

namespace name1
{
	int ns_int2 = 0;
}


void try_function(void) throw()
{
	int a = 0x55;

	try
	{
		throw a;
	}

	catch (double a)
	{
		cout << "catch double ok 1:" <<hex<<a<< endl;
	}

	catch (...)
	{
		cout << "catch int ok=" << hex << a << endl;
		throw;	//throw the same fault again
	}

	cout << "end 1" << endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
	int sum, a, b;
	string str;

	const student student1(1, "name", "sex");
	student student2;
	student1.const_display();
	cout << "static var=" << student::static_var <<","<<student1.static_var<< endl;
	class_friend_function(student2);
	cout << "class < operator reload=" << (student1 > student2) << endl;
	cout << "type conversion function=" << int(student1) << endl;

	student_derived stu1;
	stu1.display();

	cout << "namespace int=" << name1::ns_int2<< endl;

	try
	{
		try_function();
	}
	catch (double a)
	{
		cout << "catch double ok 2:" << a << endl;
	}
	catch (...)
	{
		cout << "default catch ok" << endl;
	}

	cout << "this is a c++ program"<<endl<<"input a, b to add..."<<endl;

	cin >> a >> b;
	sum = a + b;
	cout << "a+b=" << sum << endl;

	cout << "template T max(T a,T b) return " << max(a,b) << endl;
	cout << "default parameter max(a=1,b=2) return" << max_int() << endl;

	str = "string";
	cout << "string type=" << str <<",sizeof(str)="<<sizeof(str)<< endl;

	system("pause");

	return 0;
}

C ++ lists some common grammatical usage, you can read this code inside, basically most of the c ++ syntax are familiar with

Published 30 original articles · won praise 21 · Views 140,000 +

Guess you like

Origin blog.csdn.net/oushaojun2/article/details/104775968