#include<iostream> using namespace std; class point { public: point(float a, float b) { //构造函数 x = a; y = b; } friend ostream & operator <<(ostream &, point &); //运算符重载 protected: float x, y; }; std::ostream &operator<<(std::ostream &output, point &p) { //运算符重载的定义 output << "(" << p.x << "," << p.y << ")" << endl; return output; } class circle :public point { public: circle(float a, float b, float c) :point(a, b), radius(c) {} //构造函数 friend ostream &operator <<(ostream &, circle &); protected: float radius; }; ostream &operator <<(ostream &output, circle &c) { output << "(" << c.x << "," << c.y << ")," << "radius=" << c.radius << endl; return output; } int main() { point a(2.3, 4.6); cout << "the point is:" << a << endl; circle m(0, 0, 8); cout << "the circle is" << m << endl; }
#include<iostream> using namespace std; class point { public: point(float a, float b) { //构造函数 x = a; y = b; } virtual void display(); //虚函数 protected: float x, y; }; void point::display(){ cout<<"(" << x << "," << y << ")"<<endl; } class circle :public point { public: circle(float a, float b, float c) :point(a, b), radius(c) {} //构造函数 void display(); protected: float radius; }; void circle::display(){ cout<< "(" << x << "," << y << ")," << "radius=" << radius << endl; } int main() { point a(2.3, 4.6); circle m(0, 0, 8); point *p1=&a; //基类指针 p1=&m; p1->display(); return 0; }
#include<iostream> using namespace std; class point { public: point(float x = 0, float y = 0); //声明构造函数时指定默认参数 virtual ~point() { cout << "executing point destructor" << endl; } protected: float x, y; }; point::point(float a,float b){ //在定义构造函数时可以不指定默认参数 x=a; y=b; } class circle :public point { public: circle(float x = 0, float y = 0, float r = 1) ; ~circle() { cout << "executing circle destructor" << endl; } protected: float r; }; circle::circle(float a,float b,float c){ x=a; y=b; r=c; } int main() { point *p = new circle; delete p; return 0; }
//虚函数与抽象基类的应用 #include<iostream> using namespace std; //声明抽象抽象基类shape class shape{ public: virtual float area() const {return 0;} //虚函数 virtual float volume() const {return 0;} virtual void shapname() const=0; //纯虚函数 }; //声明point类 class point:public shape { public: point(float x=0,float y=0); //声明构造函数 virtual void shapname() const {cout<<"point";} protected: float x,y; }; //定义point point::point(float a,float b){ x=a; y=b; } //声明circle类 class circle:public point{ public: circle(float x=0,float y=0,float r=0); virtual float area() const; virtual void shapname() const {cout<<"circle:";} protected: float radius; }; //定义circle类 circle::circle(float a,float b,float r):point(a,b),radius (r) {} //定义构造函数 float circle::area() const {return radius*radius*3.14;} //声明cylinder类 class cylinder:public circle{ public: cylinder(float x=0,float y=0,float r=0,float h=0); virtual float area() const; virtual float volume() const; virtual void shapname() const{cout<<"cylinder:";} protected: float height; }; //定义cylinder类 cylinder::cylinder(float a,float b,float r,float h):circle(a,b,r),height(h){} float cylinder::area() const{ return 2*circle::area()+2*radius*3.14*height; } float cylinder::volume() const{ return height*circle::area(); } //main函数 int main(){ point point(0,0); circle circle(0,0,3); cylinder cylinder(2,2,1,1); point.shapename; //静态关联(通过对象名调用虚函数)在编译阶段就可以确定调用的是哪一类的虚函数 shape *pt; //定义基类指针 pt=&point; //使指针指向point类 pt->shapname(); //用指针建立动态关联 (在编译阶段无法从语句本身确定调用的是哪个类的虚函数) cout<<"\n"; pt=&circle; pt->shapname(); cout<<"area="<<pt->area()<<"\n"<<endl; pt=&cylinder; pt->shapname(); cout<<"area="<<pt->area()<<"\n" <<"volume="<<pt->volume()<<endl; return 0; }
原文:https://www.cnblogs.com/lanjieduanxin/p/11695520.html