您好,登錄后才能下訂單哦!
#include<iostream> using namespace std; int main() { int i1=1; double d1=3.21; cout<<"int->"<<i1<<endl<<"double->"<<d1<<endl; system("pause"); } #include<iostream> using namespace std; class person { public: void Display() { cout<<"name: "<<_name<<endl; cout<<"sex: "<<_sex<<endl; cout<<"age: "<<_age<<endl; } char* _name; char* _sex; int _age; }; int main() { person p; p._name="zhangsan"; p._sex="nan"; p._age=31; p.Display (); system("pause"); return 0; } 實例化 #include<iostream> using namespace std; class person { public: void Display() { cout<<"name: "<<_name<<endl; cout<<"sex: "<<_sex<<endl; cout<<"age: "<<_age<<endl; } public: char* _name; char* _sex; int _age; }; void test() { person p; p._name ="hhh"; p._sex ="男"; p._age =20; p. Display(); } int main() { test(); system("pause"); return 0; } 日期類 構造函數(shù) #include<iostream> using namespace std; class Date { public : Date(int year,int month,int day) { cout<<"構造函數(shù)"<<endl; _year=year; _month=month; _day=day; } void SetDate(int year,int month,int day) { _year=year; _month=month; _day=day; } void Display() { cout<<_year<<"-"<<_month<<"-"<<_day<<endl; } private : int _year; int _month; int _day; }; void test1() { Date d1(2015,2,11); d1.Display (); } int main() { test1(); system("pause"); return 0; } 重載 #include<iostream> using namespace std; class Date { public : Date(int year,int month,int day) { cout<<"構造函數(shù)"<<endl; _year=year; _month=month; _day=day; } Date(int year,int month) { _year=year; _month=month; _day=11; } Date(int year) { _year=year; _month=12; _day=11; } void SetDate(int year,int month,int day) { _year=year; _month=month; _day=day; } void Display() { cout<<_year<<"-"<<_month<<"-"<<_day<<endl; } private : int _year; int _month; int _day; }; void test1() { Date d1(2015,2,11); Date d2(2015,2); Date d3(2015); d1.Display (); d2.Display (); d3.Display (); } int main() { test1(); system("pause"); return 0; } 拷貝構造 #include<iostream> using namespace std; class Date { public : Date(int year,int month,int day); Date(const Date& d) { cout<<"拷貝構造函數(shù)"<<endl; _year=d._year ; _month=d._month ; _day=d._day ; } void SetDate(int year,int month,int day) { _year=year; _month=month; _day=day; } void Display() { cout<<_year<<"-"<<_month<<"-"<<_day<<endl; } private : int _year; int _month; int _day; }; Date::Date(int year,int month,int day) { cout<<"構造函數(shù)"<<endl; _year=year; _month=month; _day=day; } void test1() { Date d1(2015,2,11); d1.Display (); Date d2(d1); d2.Display (); } int main() { test1(); system("pause"); return 0; } 析構函數(shù) #include<iostream> using namespace std; class Arr { public: Arr(size_t size) { _prt=(int*)malloc(sizeof(int)*size); } ~Arr() { free(_prt); } private: int* _prt; }; int main() { Arr(20); system("pause"); return 0; } 復數(shù)類 #include<iostream> using namespace std; class Complex { public : Complex(double real=0,double p_w_picpath=0) { cout<<"Complex (double real=0,double p_w_picpath=0)"<<endl; _real=real; _p_w_picpath=p_w_picpath; } ~Complex() { cout<<"~Complex()"<<endl; } Complex(const Complex& d) { cout<<"Complex(comst Complex& d)"<<endl; _real=d._real ; _p_w_picpath=d._p_w_picpath ; } Complex& operator=(const Complex& d) { cout<<"operator=(const Complex& d)"<<endl; if(this!=&d) { this->_real=d._real; this->_p_w_picpath=d._p_w_picpath; } return *this; } void Display() { cout<<"Real:"<<_real<<"--Image:"<<_p_w_picpath<<endl; } private: double _real; double _p_w_picpath; }; void test() { Complex d1(1.1,3.3); d1.Display (); } int main() { test(); system("pause"); return 0; }
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。