[C++]Introduction to C++

2019-9-30 写技术

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

class Shap{
private:
	int fulfill;
	int color;
	double pi;

	/* Mutable data member can be exchanged in const object. */
	mutable int count;
	/* Const data member will handicape the copying of object,
	 * and the const data member have to defined in constructor
	 * function in method of parameters table. */
	const int hour;

	static int minute;

public:
	/* A class can have only one default constructor method 
	 * Shap() and Shap(double=2) will cause a redefined warnning. */
	//Shap();
	Shap(double=2); /* Constructor */
	Shap(int color); /* Overloading */
	Shap(const Shap &s); /* Copy constructor */
	~Shap(); /* Destructor */
	int isFulfill();

	/* Overloading */
	Shap& operator-(int);
	Shap& operator~();
	friend Shap& operator-(Shap &);
	/* Type exchanging function. (The negative operation of exchanging constructor function) */
	operator double();

	double area();
	inline double getPi();
	virtual void display();
	void show();
	void show_const() const;
	void count_add() const;

	static void minute_add();
};

/* Static data member should be initiallized out of the class. */
int Shap::minute = 5555;

#if 0
Shap::Shap()
{
	fulfill = 1;
	this->pi = 3.14;
	cout<<"Shap init"<<endl;
}
#endif

/* Parameters table is used in defined only.  */
Shap::Shap(int color):color(color),hour(color)
{
	fulfill = 1;
	this->pi = 3.14;
	cout<<"Shap init with parameters table. color:"<<color<<endl;
	//color = color;
	count = 1;
}

Shap::Shap(double d):color(d),hour(d)
{
	fulfill = 1;
	this->pi = 3.14;
	cout<<"Shap init with double"<<endl;
}

Shap::Shap(const Shap &s):hour(1)
{
	color = s.color;
	cout<<"Copy constructor."<<endl;
}

Shap::~Shap()
{
	cout<<"destruct:"<<"Shap. color:"<<color<<endl;
}

Shap& Shap::operator-(int c)
{
	color-=c;
	return *this;
}

Shap& Shap::operator~()
{
	fulfill = !fulfill;
	return *this;
}

Shap& operator-(Shap &s)
{
	~s;
	return s;
}

int Shap::isFulfill()
{
	return this->fulfill;	
}

void Shap::show()
{
	cout<<"Shap color:"<<color<<endl;
	cout<<"Shap minute:"<<minute<<endl;
}

void Shap::show_const() const
{
	cout<<"show const :"<<color<<endl;
}

void Shap::count_add() const
{
	cout<<"count add: "<<++count<<endl;
}

void Shap::minute_add()
{
	minute++;
}

Shap:: operator double()
{
	return color;
}

class Circle:public Shap{
private:
	double r;

public:
	Circle(double);
	~Circle();
	double area();
	void display();
};

Circle::Circle(double r):r(r){
	cout<<"circle init"<<endl;
}

Circle::~Circle()
{
	cout<<"destruct circle"<<endl;
}

double Circle::area()
{
	return getPi()*r*r;
}

void Circle::display()
{
	cout<<"circle display"<<endl;
}


/* Inline member function must be defined distinctly out of the class. 
 * But it's not necessary defined in class. */
inline double Shap::getPi()
{
	return pi;
}

double Shap::area()
{
	return 0;
}

void Shap::display()
{
	cout<<"Shap display"<<endl;
}


class Student;

/* Template defining command must be folled by class.  */
template <class numtype>
class People{
private:
	numtype age;

public:
	People(numtype a)
	{
		age = a;
	}
	People()
	{
		age = 1;
		cout<<"Constructor of People."<<endl;
	}
	void display(){
		cout<<"People age:"<<age<<endl;
	}
	void show();
	friend Student;
	friend void show_all_people(People <int> &p);

	virtual void work();
	//void work();
};

class Student{
public:
	void display(){
		cout<<"num:"<<num<<endl;
		//cout<<"name:"<<name<<endl;
		cout<<"sex:"<<sex<<endl;
	}

	void show_people(People <int> &s)
	{
		cout<<"Friend show age:"<<s.age<<endl;
		s.display();
	}

private:
	int num;
	string name;
	char sex;
};

class Teacher:public People<int>{
private:
	int age;
public:
	Teacher(int age, int grade);
	void work();
};

class Professor:public People<int>{
public:
	void work();
};

Teacher::Teacher(int age, int grade):People(age){
	cout<<"Teacher age grade."<<endl;
}

void show_all_people(People <int> &p)
{
	cout<<"show all people:"<<p.age<<endl;
}

void People<int>::work()
{
	cout<<"I'm just a people."<<endl;
}

void People<int>::show()
{
	cout<<"People show."<<endl;
}

void Teacher::work()
{
	cout<<"My work is teacher. "<<age<<endl;
}

void Professor::work()
{
	cout<<"My work is professor."<<endl;
}


/* The class member is defined as private member default. */
class cup{
	int color;
	friend void show_cup(cup &);
	void display()
	{
		cout<<"color:"<<color<<endl;
	}
};

void show_cup(cup &c)
{
	c.display();
	c.color = 1;
	c.display();
}

/* This is a class without name, so it has no construct function. */
class{
public:
	void display()
	{
		color = 2;
		cout<<"towel1 color:"<<color<<endl;
	}
private:
	int color;
}towel1;

/* An example of destruct in order. */
void destruct_shap()
{
	Shap s1(1);
	Shap s2(2);

	Shap ss[6] = {1,2,3.0,4,5};
	Shap *p = new Shap(88);

	/* A pointer to the member method. */
	void (Shap:: *pFun)();
	pFun = &Shap::show;
	(p->*pFun)();
	(s1.*pFun)();
	cout<<"minus"<<endl;
	p->show();
	/* Const data member will handicape the copying of class object */
	//*p = *p - 1;
	p->show();
	cout<<"minus over"<<endl;
	delete p;

	const Shap s3(3);
	s3.show_const();
	s3.count_add();
	const Shap *p3 = &s3;
	p3->show_const();
	//p3->display(); Error, const object pointer can't call normal member.

	Shap s4 = s3;
	s4.display();
	s4.minute_add();
	s4.show();
	Shap::minute_add();
	s4.show();

	cout<<"Shap to double :"<<double(s4)<<endl;
}

class A{
public:
	int data;
	A(){
		data = 1;
	}
	A(int s){
		data = s;
	}
};

class B:virtual public A{
public:
	int data_b;
	B(int s):A(s){
		data_b = s;
	}
};

class C:virtual public A{
public:
	int data_c;
	C(int s):A(s){
		data_c = s;
	}
};

class SUB{
public:
	int data_sub;
	SUB(){
		data_sub = 999;
	}
	SUB(int s){
		data_sub = s;
	}
};

class D:public B, public C{
public:
	int data_d;
	SUB sub;
	D():B(2),C(3),A(11),sub(9999){
		data_d = 666;
	}
	void show(A pa){
		cout<<data<<" "<<data_b<<"  "<<data_d<<endl;
		cout<<sub.data_sub<<endl;
		B bb(2);
		A aa(1);
		aa = bb;
		// error: bb = aa;
		A &aaa = bb;
		cout<<aaa.data<<endl;
	}
};


int main()
{
	cout<<"Hello world"<<endl;
	Shap *s = new Shap();
	cout<<s->isFulfill()<<endl;
	cout<< (-*s).isFulfill()<<endl;
	cout<< (-*s).isFulfill()<<endl;
	cout<< "Shap area:"<< s->area()<<endl;
	s->show();

	Shap(3).show();
	Shap(3.0).show();

	cout<<"circle init"<<endl;
	Circle c(4);
	cout<< "circle area:"<< c.area()<<"  fullfill:"<<c.isFulfill()<<endl;

	Shap *pt = s;
	pt->display();
	pt = &c;
	pt->display();
	c.display();
	((Circle *)pt)->display();


	delete s;
	destruct_shap();

	cup cups;
	//cups.display();
	show_cup(cups);

	towel1.display();
	

	People <int> pe1(568);
	Student se1;
	se1.show_people(pe1);
	show_all_people(pe1);
	pe1.show();

	Teacher t1(28,1);
	t1.display();
	Professor pr1;
	People<int> *pp = &pe1;
	pp->work();
	pp = &t1;
	pp->work();
	pp = &pr1;
	pp->work();

	cout<<"Easy testing..."<<endl;
	cout<<setfill('*');
	cout<<dec<<hex<<oct<<setfill('*')<<setprecision(5);
	cout<<setiosflags(ios::fixed);
	cout<<setiosflags(ios::scientific);
	cout<<setiosflags(ios::left);
	cout<<setiosflags(ios::right);
	cout<<setiosflags(ios::skipws);
	cout<<setiosflags(ios::uppercase);
	cout<<setiosflags(ios::showpos);
	cout<<hex<<11<<oct<<"  "<<11<<" "<<11<<" "<<setprecision(3)<<1.123456<<endl;
	cout<<setw(8);
	cout<<2<<"  "<<1.123<<endl;
	//cout<<char(getchar())<<endl;
	cout<<"Testing end."<<endl;
	
	int ii = 0xffffffff00000001; /* The high part will be lost. So it equals 0x00000001. */
	short ss = 0xffff;	/* It means -1 in decimacial. */
	long long iii = 0x7fffffff00000001; 

	cout<<"short "<<sizeof(short)<<endl;
	cout<<"int "<<sizeof(int)<<endl;
	cout<<"long "<<sizeof(long)<<endl;
	cout<<"long long "<<sizeof(long long)<<endl;
	cout<<"float "<<sizeof(float)<<endl;
	cout<<"long float "<<sizeof(long float)<<endl;
	cout<<"double "<<sizeof(double)<<endl;
	cout<<"long double "<<sizeof(long double)<<endl;
	cout<<"bool "<<sizeof(bool)<<endl;
	cout<<"char "<<sizeof(char)<<endl;

	cout<<setiosflags(ios::fixed);
	cout<<setiosflags(ios::fixed)<<0.11e1f<<endl;
	cout<<0.12<<endl;

	cout<<dec;
	cout<<ii<<"  "<<ss<<"  "<<iii<<endl;
	ii = ss;
	//ss = ii;
	cout<<ii<<"  "<<ss<<"  "<<iii<<endl;

	ss = 0xffff;
	cout<<hex<<ss<<"  "<<dec<<ss<<endl;
	ii = 0xffff;
	cout<<hex<<ii<<"  "<<dec<<ii<<endl;

	D ddd;
	ddd.show(ddd);

	cout<<dec<<setiosflags(~ios::scientific)<<2575.5*3<<endl;

	getchar();
	getchar();

        /* log.anycle.com */

	return 0;
}


标签: C++

发表评论:

Powered by anycle 湘ICP备15001973号-1