文章目录
- 目录
- 1.继承
- 2.重载
- 3.多态 && 虚函数
目录
1.继承
#include <iostream>using namespace std;// 基类
class Shape
{public:void setWidth(int w){width = w;}void setHeight(int h){height = h;}protected:int width;int height;
};// 派生类
class Rectangle: public Shape
{public:int getArea(){ return (width * height); }
};int main(void)
{Rectangle Rect;Rect.setWidth(5);Rect.setHeight(7);// 输出对象的面积cout << "Total area: " << Rect.getArea() << endl;return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
Total area: 35
#include <iostream>using namespace std;// 基类 Shape
class Shape
{public:void setWidth(int w){width = w;}void setHeight(int h){height = h;}protected:int width;int height;
};// 基类 PaintCost
class PaintCost
{public:int getCost(int area){return area * 70;}
};// 派生类
class Rectangle: public Shape, public PaintCost
{public:int getArea(){ return (width * height); }
};int main(void)
{Rectangle Rect;int area;Rect.setWidth(5);Rect.setHeight(7);area = Rect.getArea();// 输出对象的面积cout << "Total area: " << Rect.getArea() << endl;// 输出总花费cout << "Total paint cost: $" << Rect.getCost(area) << endl;return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
Total area: 35
Total paint cost: $2450
#include <iostream>using namespace std;
//基类class D
{
public:D(){cout<<"D()"<<endl;}~D(){cout<<"~D()"<<endl;}
protected:int d;
};class B:virtual public D
{
public:B(){cout<<"B()"<<endl;}~B(){cout<<"~B()"<<endl;}
protected:int b;
};class A:virtual public D
{
public:A(){cout<<"A()"<<endl;}~A(){cout<<"~A()"<<endl;}
protected:int a;
};class C:public B, public A
{
public:C(){cout<<"C()"<<endl;}~C(){cout<<"~C()"<<endl;}
protected:int c;
};int main()
{cout << "Hello World!" << endl;C c; //D, B, A ,Ccout<<sizeof(c)<<endl;return 0;
}
Hello World!
D()
B()
A()
C()
40
~C()
~A()
~B()
~D()
2.重载
#include <iostream>
using namespace std;class printData
{public:void print(int i) {cout << "整数为: " << i << endl;}void print(double f) {cout << "浮点数为: " << f << endl;}void print(string c) {cout << "字符串为: " << c << endl;}
};int main(void)
{printData pd;// 输出整数pd.print(5);// 输出浮点数pd.print(500.263);// 输出字符串pd.print("Hello C++");return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
整数为: 5
浮点数为: 500.263
字符串为: Hello C++
#include <iostream>
using namespace std;class Box
{public:double getVolume(void){return length * breadth * height;}void setLength( double len ){length = len;}void setBreadth( double bre ){breadth = bre;}void setHeight( double hei ){height = hei;}// 重载 + 运算符,用于把两个 Box 对象相加Box operator+(const Box& b){Box box;box.length = this->length + b.length;box.breadth = this->breadth + b.breadth;box.height = this->height + b.height;return box;}private:double length; // 长度double breadth; // 宽度double height; // 高度
};
// 程序的主函数
int main( )
{Box Box1; // 声明 Box1,类型为 BoxBox Box2; // 声明 Box2,类型为 BoxBox Box3; // 声明 Box3,类型为 Boxdouble volume = 0.0; // 把体积存储在该变量中// Box1 详述Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0);// Box2 详述Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0);// Box1 的体积volume = Box1.getVolume();cout << "Volume of Box1 : " << volume <<endl;// Box2 的体积volume = Box2.getVolume();cout << "Volume of Box2 : " << volume <<endl;// 把两个对象相加,得到 Box3Box3 = Box1 + Box2;// Box3 的体积volume = Box3.getVolume();cout << "Volume of Box3 : " << volume <<endl;return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
#include <iostream>
using namespace std;class Distance
{private:int feet; // 0 到无穷int inches; // 0 到 12public:// 所需的构造函数Distance(){feet = 0;inches = 0;}Distance(int f, int i){feet = f;inches = i;}// 显示距离的方法void displayDistance(){cout << "F: " << feet << " I:" << inches <<endl;}// 重载负运算符( - )Distance operator- () {feet = -feet;inches = -inches;return Distance(feet, inches);}
};
int main()
{Distance D1(11, 10), D2(-5, 11);-D1; // 取相反数D1.displayDistance(); // 距离 D1-D2; // 取相反数D2.displayDistance(); // 距离 D2return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
F: -11 I:-10
F: 5 I:-11
3.多态 && 虚函数
#include <iostream>
using namespace std;class Shape {protected:int width, height;public:Shape( int a=0, int b=0){width = a;height = b;}int area(){cout << "Parent class area :" <<endl;return 0;}
};
class Rectangle: public Shape{public:Rectangle( int a=0, int b=0):Shape(a, b) { }int area (){ cout << "Rectangle class area :" <<endl;return (width * height); }
};
class Triangle: public Shape{public:Triangle( int a=0, int b=0):Shape(a, b) { }int area (){ cout << "Triangle class area :" <<endl;return (width * height / 2); }
};
// 程序的主函数
int main( )
{Shape *shape;Rectangle rec(10,7);Triangle tri(10,5);// 存储矩形的地址shape = &rec;// 调用矩形的求面积函数 areashape->area();// 存储三角形的地址shape = &tri;// 调用三角形的求面积函数 areashape->area();return 0;
}