类和对象的实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类和对象的实现相关的知识,希望对你有一定的参考价值。
#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; } 日期类 构造函数 #include<iostream> using namespace std; class Date { public : Date(int year,int month,int day) { cout<<"构造函数"<<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<<"构造函数"<<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<<"拷贝构造函数"<<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<<"构造函数"<<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; } 析构函数 #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; } 复数类 #include<iostream> using namespace std; class Complex { public : Complex(double real=0,double image=0) { cout<<"Complex (double real=0,double image=0)"<<endl; _real=real; _image=image; } ~Complex() { cout<<"~Complex()"<<endl; } Complex(const Complex& d) { cout<<"Complex(comst Complex& d)"<<endl; _real=d._real ; _image=d._image ; } Complex& operator=(const Complex& d) { cout<<"operator=(const Complex& d)"<<endl; if(this!=&d) { this->_real=d._real; this->_image=d._image; } return *this; } void Display() { cout<<"Real:"<<_real<<"--Image:"<<_image<<endl; } private: double _real; double _image; }; void test() { Complex d1(1.1,3.3); d1.Display (); } int main() { test(); system("pause"); return 0; }
以上是关于类和对象的实现的主要内容,如果未能解决你的问题,请参考以下文章