1.构造函数举例
#includeusing namespace std; class cube { public: double vol() { return x * y * z; }; cube(double a, double b, double c); private: double x, y, z; }; cube::cube(double a, double b, double c) { x = a; y = b; //利用构造函数来初始化对象中数据成员的值 z = c; } int main() { cube a(1, 2, 5); cube b(5, 63, 3.2); cout << a.vol() << endl; cout << b.vol() << endl; return 0; }
2.构造函数初始化列表举例
#includeusing namespace std; class cube { public: double vol() { return x * y * z; }; cube(double a, double b, double c); private: double x, y, z; }; cube::cube(double a, double b, double c) :x(a),y(b),z(c) //初始化列表 { } int main() { cube a(1, 2, 5); cube b(5, 63, 3.2); cout << a.vol() << endl; cout << b.vol() << endl; return 0; }
3.构造函数重载举例 两个叫point的构造函数 不同的方法来初始化。
#includeusing namespace std; class point { public: void print() { cout << x << endl; cout << y << endl; } point(int i, int j) :x(i), y(j) {} point() { x = y = 0; } private: int x,y; }; int main() { point a; point b(1, 2); a.print(); b.print(); return 0; }
4.带默认参数的构造函数
#includeusing namespace std; class point { public: void print() { cout << x << " "; cout << y << endl; } point(int i=0, int j=0) :x(i), y(j) {} private: int x,y; }; int main() { point a; point b(1, 2); point c(1); point d(2); a.print(); b.print(); c.print(); d.print(); return 0; }
5.深复制与浅复制举例
#include#include using namespace std; class ca { public: ca(int b, char* cstr) { a = b; str = new char[b]; strcpy(str, cstr); } ca(const ca& c) { a = c.a; str = new char[a]; if (str != 0)strcpy(str, c.str); } void show() { cout << str << endl; } ~ca() { delete str; } private: int a; char* str; }; int main() { ca a(10, "sdsds"); ca b = a; b.show(); return 0; }
6.this指针举例
7.赋值兼容规则举例
此程序花里胡哨,因为照抄的老师。
关键点在于看work函数的形参的三种不同形式,代表了赋值兼容规则的三种情况,派生类对象赋值给1基类对象,派生类赋值给基类的2引用和3指针
#includeusing namespace std; class man { protected: int age; string name; public: man(int a,string n):age(a),name(n){} void print() { cout << "姓名:" << name << ",年龄:" << age << endl; } int get_age() { return age; } string get_name() { return name; } }; class superman :public man { private: int force_value; public: superman(int a,string n,int f):man(a,n),force_value(f){} void print() { cout << "姓名是" << name << "年龄是" << age << "武力值为" << force_value << endl; } void fly() { cout << "fly fly 82002222" << endl; } }; int work(man a) { if (a.get_age() >= 20)return 1; return 0; } int main() { int n; man i(25, "武则天"); superman j(22, "ssssss", 200); n = work(j); if (n != 0)cout << j.get_name() << "牛逼" << endl; else cout << j.get_name() << "垃圾" << endl; return 0; }
int work(man *a)
{
if (a->get_age() >= 20)return 1;
return 0;
}
int main()
{
int n;
man i(25, "武则天");
superman j(22, "ssssss", 200);
n = work(&j);
if (n != 0)cout << j.get_name() << "牛逼" << endl;
else cout << j.get_name() << "垃圾" << endl;
return 0;
}
int work(man &a)
{
if (a.get_age() >= 20)return 1;
return 0;
}
int main()
{
int n;
man i(25, "武则天");
superman j(22, "ssssss", 200);
n = work(j);
if (n != 0)cout << j.get_name() << "牛逼" << endl;
else cout << j.get_name() << "垃圾" << endl;
return 0;
}
35.1析构函数举例
构造函数和析构函数调用顺序相反。
#includeusing namespace std; class A { public: A() { cout << "A constructor" << endl; } ~A() { cout << "A destructor" << endl; } }; class B :public A { public: B() { cout << "B constructor" << endl; } ~B() { cout << "B destructor" << endl; } }; class C :public B { public: C() { cout << "C constructor" << endl; } ~C() { cout << "C destructor" << endl; } }; int main() { C t1; return 0; }
35.2析构函数和构造函数举例2
没啥可说的,很简单的输入输出程序,读一下就行。
就是学生的一个类,里边有学生信息
#include#include using namespace std; class Undergraduate { protected: int num; string name; char sex; public: Undergraduate(int n, string nam, char se) { num = n; name = nam; sex = se; } ~Undergraduate(){} }; class garduate_stu :public Undergraduate { private: int age; string addr; public: garduate_stu(int n, string nam, char se, int ag, string add) :Undergraduate(n, nam, se) { age = ag; addr = add; } void show() { cout << "num = " << num << " name =" << name << "sex= " << sex << "age= " << age << " address=" << addr << endl; } ~garduate_stu(){} }; int main() { garduate_stu stu1(0021, "li yang", '1', 23, " hebeis"); garduate_stu stu2(0022, "liu li", '1', 20, " hesssssssbeis"); stu1.show(); stu2.show(); return 0; }
36.1多重继承举例
#include#include using namespace std; class base1 { private: int b1; public: base1() { b1 = 0; cout << "默认构造函数base1: b1 = " << b1 << endl; } base1(int i):b1(i){ cout << "构造函数base1: b1 = " << b1 << endl; } }; class base2 { private: int b2; public: base2() { b2 = 0; cout << "默认构造函数base2: b2 = " << b2 << endl; } base2(int j) :b2(j) { cout << "构造函数base2: b2 = " << b2 << endl; } }; class base3 { public: base3() { cout << "默认构造函数base3" << endl; } }; class Derive :public base1, public base2, public base3 { private: base1 memberbase1; base2 memberbase2; base3 memberbase3; public: Derive() { cout <<"默认构造函数Derive "<< endl; } Derive(int a,int b,int c,int d):base1(a),base2(b),memberbase1(c),memberbase2(d){ cout<<"构造Derive" << endl; } }; int main() { cout<<"创建派生类Derive对象obj1" << endl; Derive obj1; cout << "创建派生类Derive对象obj2" << endl; Derive obj2(1,2,3,4); return 0; }
程序运行结果如下:关注调用顺序以及调用的构造函数还是默认构造函数。
创建派生类Derive对象obj1
默认构造函数base1: b1 = 0
默认构造函数base2: b2 = 0
默认构造函数base3
默认构造函数base1: b1 = 0
默认构造函数base2: b2 = 0
默认构造函数base3
默认构造函数Derive
创建派生类Derive对象obj2
构造函数base1: b1 = 1
构造函数base2: b2 = 2
默认构造函数base3
构造函数base1: b1 = 3
构造函数base2: b2 = 4
默认构造函数base3
构造Derive
36.2虚基类举例
重点关注A的fun函数,被B1和B2继承,而C又继承了B1和B2,因此相当于C有两个fun函数,因此需要在A类的所有直接派生类中声明virtual。这样就不会产生二义性了。
#include#include using namespace std; class A { public: A(int n) :nv(n) { cout << "调用A的构造函数" << endl; } void fun() { cout<<"fun of A" << endl; } private: int nv; }; class B1:virtual public A{ public: B1(int a) :A(a) { cout<<"派生类B1的构造函数" << endl; } private: int nv1; }; class B2 :virtual public A { public: B2(int a) :A(a) { cout << "派生类B2的构造函数" << endl; } private: int nv2; }; class C :public B1, public B2 { public: C(int a) :A(a), B1(a), B2(a) { cout<<"C类的构造函数"<< endl; } void fund() { cout<<"fun of C" << endl; } private: int nvd; }; int main() { C c1(1); c1.fund(); c1.fun(); return 0; }
运行结果:
调用A的构造函数
派生类B1的构造函数
派生类B2的构造函数
C类的构造函数
fun of C
fun of A
36.4多重继承应用举例
#include#include using namespace std; enum Color {Red,Yellow,Green,White}; class Circle { private: float radius; public: Circle(float r) :radius(r) { cout<<"circle initialized" << endl; } ~Circle() { cout<<"circle destroyed" << endl; } float Area() { return 3.1415926 * radius * radius; } }; class Table { float height; public: Table(float h) :height(h) { cout<<"Table initialized "<< endl; } ~Table() { cout << "Table destroyed" << endl; } float Height() { return height; } }; class RoundTable :public Table, public Circle { Color color; public: RoundTable(float h, float r, Color c) :Table(h), Circle(r), color(c) { cout << "RoundTable initialized ! " << endl; } int GetColor() { return color; } ~RoundTable() { cout << "RoundTable destroyed" << endl; } }; int main() { RoundTable cir_table(2.3, 5.3, Yellow); cout << "The table properties are" << endl; cout << "高度Height为" << cir_table.Height() << endl; cout << "面积Area为" << cir_table.Area() << endl; cout << "颜色color为" << cir_table.GetColor() << endl; return 0; }



