类和对象的实现
#include<iostream>usingnamespacestd;intmain(){inti1=1;doubled1=3.21;cout<<"int->"<<i1<<endl<<"double->"<<d1<<endl;system("pause");}#include<iostream>usingnamespacestd;classperson{public:voidDisplay(){cout<<"name:"<<_name<<endl;cout<<"sex:"<<_sex<<endl;cout<<"age:"<<_age<<endl;}char*_name;char*_sex;int_age;};intmain(){personp;p._name="zhangsan";p._sex="nan";p._age=31;p.Display();system("pause");return0;}实例化#include<iostream>usingnamespacestd;classperson{public:voidDisplay(){cout<<"name:"<<_name<<endl;cout<<"sex:"<<_sex<<endl;cout<<"age:"<<_age<<endl;}public:char*_name;char*_sex;int_age;};voidtest(){personp;p._name="hhh";p._sex="男";p._age=20;p.Display();}intmain(){test();system("pause");return0;}日期类构造函数#include<iostream>usingnamespacestd;classDate{public:Date(intyear,intmonth,intday){cout<<"构造函数"<<endl;_year=year;_month=month;_day=day;}voidSetDate(intyear,intmonth,intday){_year=year;_month=month;_day=day;}voidDisplay(){cout<<_year<<"-"<<_month<<"-"<<_day<<endl;}private:int_year;int_month;int_day;};voidtest1(){Dated1(2015,2,11);d1.Display();}intmain(){test1();system("pause");return0;}重载#include<iostream>usingnamespacestd;classDate{public:Date(intyear,intmonth,intday){cout<<"构造函数"<<endl;_year=year;_month=month;_day=day;}Date(intyear,intmonth){_year=year;_month=month;_day=11;}Date(intyear){_year=year;_month=12;_day=11;}voidSetDate(intyear,intmonth,intday){_year=year;_month=month;_day=day;}voidDisplay(){cout<<_year<<"-"<<_month<<"-"<<_day<<endl;}private:int_year;int_month;int_day;};voidtest1(){Dated1(2015,2,11);Dated2(2015,2);Dated3(2015);d1.Display();d2.Display();d3.Display();}intmain(){test1();system("pause");return0;}拷贝构造#include<iostream>usingnamespacestd;classDate{public:Date(intyear,intmonth,intday);Date(constDate&d){cout<<"拷贝构造函数"<<endl;_year=d._year;_month=d._month;_day=d._day;}voidSetDate(intyear,intmonth,intday){_year=year;_month=month;_day=day;}voidDisplay(){cout<<_year<<"-"<<_month<<"-"<<_day<<endl;}private:int_year;int_month;int_day;};Date::Date(intyear,intmonth,intday){cout<<"构造函数"<<endl;_year=year;_month=month;_day=day;}voidtest1(){Dated1(2015,2,11);d1.Display();Dated2(d1);d2.Display();}intmain(){test1();system("pause");return0;}析构函数#include<iostream>usingnamespacestd;classArr{public:Arr(size_tsize){_prt=(int*)malloc(sizeof(int)*size);}~Arr(){free(_prt);}private:int*_prt;};intmain(){Arr(20);system("pause");return0;}复数类#include<iostream>usingnamespacestd;classComplex{public:Complex(doublereal=0,doublep_w_picpath=0){cout<<"Complex(doublereal=0,doublep_w_picpath=0)"<<endl;_real=real;_p_w_picpath=p_w_picpath;}~Complex(){cout<<"~Complex()"<<endl;}Complex(constComplex&d){cout<<"Complex(comstComplex&d)"<<endl;_real=d._real;_p_w_picpath=d._p_w_picpath;}Complex&operator=(constComplex&d){cout<<"operator=(constComplex&d)"<<endl;if(this!=&d){this->_real=d._real;this->_p_w_picpath=d._p_w_picpath;}return*this;}voidDisplay(){cout<<"Real:"<<_real<<"--Image:"<<_p_w_picpath<<endl;}private:double_real;double_p_w_picpath;};voidtest(){Complexd1(1.1,3.3);d1.Display();}intmain(){test();system("pause");return0;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。