1.定义Person类,源程序文件名为penson.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string> 3 4 class Person 5 { 6 private: 7 char Name[20]; 8 char Sex; 9 int Age; 10 11 public: 12 void SetData (char na[], char s, int a) 13 { 14 strcpy(Name, na); 15 Sex = s; 16 Age = a; 17 } 18 19 void GetName (char *na) 20 { 21 strcpy(na, Name); 22 } 23 24 char GetSex () 25 { 26 return Sex; 27 } 28 29 int GetAge () 30 { 31 return Age; 32 } 33 };
定义对象的形式是: <类名> <对象列表>;如 Person a,b;
2.定义Person类,在类外定义成员函数,程序文件名为person.h.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string> 3 4 class Person 5 { 6 private: 7 char Name[20]; 8 char Sex; 9 int Age; 10 11 public: 12 void SetData (char [], char , int ); 13 void GetName (char *); 14 char GetSex (); 15 int GetAge (); 16 }; 17 18 void Person::SetData (char na[], char s, int a) 19 { 20 strcpy(Name, na); 21 Sex = s; 22 Age = a; 23 } 24 25 void Person::GetName (char *na) 26 { 27 strcpy(na, Name); 28 } 29 30 char Person::GetSex () 31 { 32 return Sex; 33 } 34 35 int Person::GetAge () 36 { 37 return Age; 38 }
3.测试Person类.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "Person.h" 2 3 using namespace std; 4 5 void main () 6 { 7 Person a, *pa; 8 char name[20]; 9 10 a.SetData("Cheng", 'F', 20); 11 a.GetName(name); 12 cout << "Name: " << name << endl; 13 cout << "Sex " << a.GetSex() << endl; 14 cout << "Age: " << a.GetAge() << endl; 15 pa = &a; 16 pa->SetData("Zhang", 'M', 18); 17 pa->GetName(name); 18 cout << "Name: " << name << endl; 19 cout << "Sex: " << pa->GetSex() << endl; 20 cout << "Age: " << pa->GetAge() << endl; 21 }
结果:
Name: Cheng
Sex: F
Age: 20
Name: Zhang
Sex: M
Age: 18
4.定义并测试长方形类CRect,长方形右左上角坐标(left, top)和右下角坐标(right, bottom)组成.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cmath> 3 4 using namespace std; 5 6 class CRect 7 { 8 private: 9 int left, top, right, bottom; 10 11 public: 12 void setcoord (int , int, int, int ); 13 void getcoord (int *L, int *T, int *R, int *B) 14 { 15 *L = left; 16 *T = top; 17 *R = right; 18 *B = bottom; 19 } 20 21 void print (void) 22 { 23 cout << "Area = " << abs(right - left) * abs(bottom - top) << endl; 24 } 25 }; 26 27 void CRect::setcoord (int L, int T, int R, int B) 28 { 29 left = L; 30 top = T; 31 right = R; 32 bottom = B; 33 } 34 35 void main (void) 36 { 37 CRect r, rr; 38 int a, b, c, d; 39 r.setcoord(100, 300, 50, 200); 40 r.getcoord(&a, &b, &c, &d); 41 cout << "left=" << a << ' ' << "top=" << b << endl; 42 cout << "right=" << c << ' ' << "bottom=" << d << endl; 43 r.print(); 44 rr = r; 45 rr.print(); 46 }
结果:
left=100 top=300
right=50 bottom=200
Area=5000
Area=5000
5.自身类的对象不可以作为成员,而自身类的指针或引用可以作为类的成员.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Obj 2 { 3 int x; 4 double y; 5 }; 6 7 class Person 8 { 9 char Name[20]; 10 char Sex; 11 int Age; 12 Obj obj1, obj2; //可以 13 Person a, b; //不可以 14 Person *pa; //可以 15 };
6.当B类对象的指针或引用作为A类的成员时,如果B类的定义在A类之后,则在A类的定义之前,必须对B类做一个引用性说明.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class B; 2 3 class A 4 { 5 private: 6 B *pb; 7 8 public: 9 ... 10 }; 11 12 class B 13 { 14 ... 15 };
7.结构体变量的成员赋始值可以在定义结构体变量的同时进行,而对于类,如果其成员是公有的,才能在定义对象时进行赋初值.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class CRect 2 { 3 public: 4 int left, top, right, bottom; 5 }; 6 7 CRect r = {100, 300, 50, 200};
8.定义日期类,利用构造函数初始化数据成员.程序放在头文件date.h中
头文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 3 using namespace std; 4 5 class Date 6 { 7 int Year, Month, Day; 8 9 public: 10 Date (int y = 2003, int m = 1, int d = 1) 11 { 12 Year = y; 13 Month = m; 14 Day = d; 15 } 16 17 void ShowDate (void) 18 { 19 cout << Year << '.' << Month << '.' << Day << endl; 20 } 21 };
源文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "date.h" 2 3 void main (void) 4 { 5 Date d1, d2(2008), d3(2008, 10), d4(2008, 10, 6); 6 7 d1.ShowDate(); 8 d2.ShowDate(); 9 d3.ShowDate(); 10 d4.ShowDate(); 11 }
结果:
2003.1.1
2008.1.1
2008.10.1
2008.10.6
Mark:
构造函数的特点如下:
1.构造函数是成员函数,函数体可写在类体内,也可写在类体外.
2.构造函数是特殊函数,该函数的名字与类名相同,不指定返回值类型.
3.构造函数可以重载,即可以定义多个参数个数不同的构造函数.
4.在创建对象时,系统自动调用构造函数.
在类体内定义构造函数的一般格式是: ClassName (<形参列表>) {...}
在类体外定义构造函数的一般格式是: ClassName::ClassName (<形参列表>) {...}
9.定义学生类,利用构造函数初始化数据成员,利用析构函数做清理工作.
头文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 class Student 7 { 8 char Num[10], *Name; 9 int Score; 10 11 public: 12 Student (char *nump, char *namep, int score) 13 { 14 if (nump) 15 strcpy(Num, nump); //在构造函数中,不需要动态申请Num成员的空间 16 else 17 strcpy(Num, ""); 18 if (namep) 19 { 20 Name = new char [strlen(namep)+1]; //在构造函数中,需要动态申请Name成员的空间 21 strcpy(Name, namep); 22 } 23 else 24 Name = NULL; 25 Score = score; 26 cout << "Constructor Called! "; 27 } 28 29 ~Student (void) 30 { 31 if (Name) 32 delete [] Name; 33 cout << "Destructor Called! "; 34 } 35 36 void Show (void) 37 { 38 cout << Num << ' ' << Name << ' ' << Score << endl; 39 } 40 };
源文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "student.h" 2 3 void main (void) 4 { 5 Student a("040120518", "George", 80); 6 a.Show(); 7 }
结果:
Constructor Called!
040120518 George 80
Destructor Called!
Mark:
析构函数的特点如下:
1.析构函数是成员函数,可在类体内定义,也可在类体外定义.
2.一般地,将析构函数定义成共有成员函数.
3.析构函数也是特殊函数,该函数的名字是类名前加~,用来与构造函数区分,该函数不指定返回值类型,没有参数.
4.一个类只能定义一个析构函数,即析构函数不允许重载.
5.析构函数可以被显示调用,也可以由系统自动调用.