写了一个简单的类继承程序
#include <iostream>
#define PI 3.14159
using namespace std;
class point
{
private: int a;
int b;
public:
point(int m,int n):a(m),b(n){
};
show(){
cout<<"该圆柱的底面圆心坐标是:"<<"\nX="<<
a<<" Y="<<b<<endl;
}
};
class circle:public point
{
int r;
public:
circle(int m,int n,int a):point(m,n){r=a;}
double getsquare(){return PI*r*r;}
virtual void display(){cout<<"圆柱底面面积为:"<<PI*r*r<<endl; //如果取消此处的virtual关键字,bk->display();ck->display();打印的都是底面积
}
};
class yuanzhu:public circle
{
int H;
public:
yuanzhu(int m,int n,int a,int b):circle(m,n,a){
H=b;
}
double getvolume(){
return getsquare()*H;
}
virtual void display(){
cout<<"圆柱的体积为:"<<getvolume()<<endl;
}
};
main()
{
yuanzhu ak(2,3,1,10);
circle m(2,3,1);
ak.display();
ak.circle::display();
ak.show();
circle *bk=&ak;
circle *ck=&m;
bk->display();
ck->display();
}
#include <iostream>
#define PI 3.14159
using namespace std;
class point
{
private: int a;
int b;
public:
point(int m,int n):a(m),b(n){
};
show(){
cout<<"该圆柱的底面圆心坐标是:"<<"\nX="<<
a<<" Y="<<b<<endl;
}
};
class circle:public point
{
int r;
public:
circle(int m,int n,int a):point(m,n){r=a;}
double getsquare(){return PI*r*r;}
virtual void display(){cout<<"圆柱底面面积为:"<<PI*r*r<<endl; //如果取消此处的virtual关键字,bk->display();ck->display();打印的都是底面积
}
};
class yuanzhu:public circle
{
int H;
public:
yuanzhu(int m,int n,int a,int b):circle(m,n,a){
H=b;
}
double getvolume(){
return getsquare()*H;
}
virtual void display(){
cout<<"圆柱的体积为:"<<getvolume()<<endl;
}
};
main()
{
yuanzhu ak(2,3,1,10);
circle m(2,3,1);
ak.display();
ak.circle::display();
ak.show();
circle *bk=&ak;
circle *ck=&m;
bk->display();
ck->display();
}
[此贴子已经被作者于2018-11-11 12:46编辑过]