C++关于派生类的问题
#include<iostream>usingnamespacestd;classDocument{public:Document(){};Document(char*n...
#include<iostream>
using namespace std;
class Document
{
public:
Document(){};
Document(char *name);
char *Name;
void PrintNameOf(){cout<<*Name;}
};
class Book:public Document
{
public:
Book(char *name,long pagecount){*Name=*name;PageCount=pagecount;}
void PrintNameOf(){cout<<"The book is:"<<*Name;}
void PrintPageCount(){cout<<"The page of the book is:"<<PageCount;}
private:
long PageCount;
};
int main()
{
Book a(computer,15);
a.PrintPageCount();
a.PrintNameOf();
return 0;
}
求大神改改,让他能正常运行。。。 展开
using namespace std;
class Document
{
public:
Document(){};
Document(char *name);
char *Name;
void PrintNameOf(){cout<<*Name;}
};
class Book:public Document
{
public:
Book(char *name,long pagecount){*Name=*name;PageCount=pagecount;}
void PrintNameOf(){cout<<"The book is:"<<*Name;}
void PrintPageCount(){cout<<"The page of the book is:"<<PageCount;}
private:
long PageCount;
};
int main()
{
Book a(computer,15);
a.PrintPageCount();
a.PrintNameOf();
return 0;
}
求大神改改,让他能正常运行。。。 展开
1个回答
展开全部
基类里的数据成员要定义成protected,派生类的继承类型为public,在矩形类中添加数据成员length和width,计算周长和面积的成员函数,直接写成CalGirth(){return 2*(length+width);}he和CalArea(){return length*width;}成员函数的类型和参数我没写,你自己填吧
这是我以前写的,改改就是你要的
#include <iostream>
using namespace std;
class Shape{//虚基类
public:
virtual float area()const{return 0.0;}
virtual float volume()const{return 0.0;}
};
class Point:public Shape{
protected:
float X;
float Y;
public:
Point();
Point(float x,float y):X(x),Y(y){}
~Point();
void setPoint(float,float);
float getX()const{return X;};
float getY()const{return Y;};
};
Point::Point(){
X=0.0;
Y=0.0;
}
Point::~Point(){}
void Point::setPoint(float x,float y){
X=x;
Y=y;
}
class Cube:public Point{ //正方体
protected:
float length;
public:
Cube();
Cube(float x,float y,float l):Point(x,y),length(l){}
~Cube();
void setLength(float);
float getLength()const;
virtual float area()const;
virtual float volume()const;
friend istream& operator >> (istream&,Cube&);
friend ostream& operator << (ostream&,Cube&);
};
Cube::Cube(){
X=0.0;
Y=0.0;
length=0.0;
}
Cube::~Cube(){}
void Cube::setLength(float l){length=l;}
float Cube::getLength()const{return length;}
float Cube::area()const{return length*length;}
float Cube::volume()const{return length*length*length;}
istream& operator >> (istream& input,Cube& c){
input>>c.X>>c.Y>>c.length;
return input;
}
ostream& operator << (ostream& output,Cube& c){
output<<"["<<c.X<<","<<c.Y<<"],length="<<c.length;
return output;
}
class Sphere:public Point{ //球体
protected:
float radius;
public:
Sphere();
Sphere(float x,float y,float r):Point(x,y),radius(r){}
~Sphere();
void setRadius(float);
float getRadius()const;
virtual float area()const;
virtual float volume()const;
friend istream& operator >> (istream&,Sphere&);
friend ostream& operator << (ostream&,Sphere&);
};
Sphere::Sphere(){
X=0.0;
Y=0.0;
radius=0.0;
}
Sphere::~Sphere(){}
void Sphere::setRadius(float r){radius=r;}
float Sphere::getRadius()const{return radius;}
float Sphere::area()const{return 4*3.14159*radius*radius;}
float Sphere::volume()const{return 4*3.14159*radius*radius*radius/3;}
istream& operator >> (istream& input,Sphere& s){
input>>s.X>>s.Y>>s.radius;
return input;
}
ostream& operator << (ostream& output,Sphere& s){
output<<"["<<s.X<<","<<s.Y<<"],r="<<s.radius;
return output;
}
class Circle:public Point{ //圆
protected:
float radius;
public:
Circle();
Circle(float x,float y,float r):Point(x,y),radius(r){}
~Circle();
void setRadius(float);
float getRadius()const;
virtual float area()const;
};
Circle::Circle(){
X=0.0;
Y=0.0;
radius=0.0;
}
Circle::~Circle(){}
void Circle::setRadius(float r){radius=r;}
float Circle::getRadius()const{return radius;}
float Circle::area()const{return 3.14159*radius*radius;}
class Cylinder:public Circle{ //圆柱体
protected:
float height;
public:
Cylinder();
Cylinder(float x,float y,float r,float h):Circle(x,y,r),height(h){}
~Cylinder();
void setHeight(float);
virtual float area()const;
virtual float volume()const;
friend istream& operator >> (istream&,Cylinder&);
friend ostream& operator << (ostream&,Cylinder&);
};
Cylinder::Cylinder(){
X=0.0;
Y=0.0;
radius=0.0;
height=0.0;
}
Cylinder::~Cylinder(){}
void Cylinder::setHeight(float h){height=h;}
float Cylinder::area()const{return 2*Circle::area()+2*3.14159*radius*height;}
float Cylinder::volume()const{return Circle::area()*height;}
istream& operator >> (istream& input,Cylinder& c){
input>>c.X>>c.Y>>c.radius>>c.height;
return input;
}
ostream& operator << (ostream& output,Cylinder& c){
output<<"["<<c.X<<","<<c.Y<<"],r="<<c.radius<<",h="<<c.height;
return output;
}
int main(){
float a,v;
Cube cube;
Sphere sphere;
Cylinder cylinder;
cout<<"请输入正方体顶点坐标,边长:";
cin>>cube;
cout<<cube<<endl;
a=cube.area();
v=cube.volume();
cout<<"area:"<<a<<"\nvolume:"<<v<<endl;
cout<<"请输入球体顶点坐标,半径:";
cin>>sphere;
cout<<sphere<<endl;
a=sphere.area();
v=sphere.volume();
cout<<"area:"<<a<<"\nvolume:"<<v<<endl;
cout<<"请输入圆柱体顶点坐标,半径,高:";
cin>>cylinder;
cout<<cylinder<<endl;
a=cylinder.area();
v=cylinder.volume();
cout<<"area:"<<a<<"\nvolume:"<<v<<endl;
system ("PAUSE");
return 0;
}
这是我以前写的,改改就是你要的
#include <iostream>
using namespace std;
class Shape{//虚基类
public:
virtual float area()const{return 0.0;}
virtual float volume()const{return 0.0;}
};
class Point:public Shape{
protected:
float X;
float Y;
public:
Point();
Point(float x,float y):X(x),Y(y){}
~Point();
void setPoint(float,float);
float getX()const{return X;};
float getY()const{return Y;};
};
Point::Point(){
X=0.0;
Y=0.0;
}
Point::~Point(){}
void Point::setPoint(float x,float y){
X=x;
Y=y;
}
class Cube:public Point{ //正方体
protected:
float length;
public:
Cube();
Cube(float x,float y,float l):Point(x,y),length(l){}
~Cube();
void setLength(float);
float getLength()const;
virtual float area()const;
virtual float volume()const;
friend istream& operator >> (istream&,Cube&);
friend ostream& operator << (ostream&,Cube&);
};
Cube::Cube(){
X=0.0;
Y=0.0;
length=0.0;
}
Cube::~Cube(){}
void Cube::setLength(float l){length=l;}
float Cube::getLength()const{return length;}
float Cube::area()const{return length*length;}
float Cube::volume()const{return length*length*length;}
istream& operator >> (istream& input,Cube& c){
input>>c.X>>c.Y>>c.length;
return input;
}
ostream& operator << (ostream& output,Cube& c){
output<<"["<<c.X<<","<<c.Y<<"],length="<<c.length;
return output;
}
class Sphere:public Point{ //球体
protected:
float radius;
public:
Sphere();
Sphere(float x,float y,float r):Point(x,y),radius(r){}
~Sphere();
void setRadius(float);
float getRadius()const;
virtual float area()const;
virtual float volume()const;
friend istream& operator >> (istream&,Sphere&);
friend ostream& operator << (ostream&,Sphere&);
};
Sphere::Sphere(){
X=0.0;
Y=0.0;
radius=0.0;
}
Sphere::~Sphere(){}
void Sphere::setRadius(float r){radius=r;}
float Sphere::getRadius()const{return radius;}
float Sphere::area()const{return 4*3.14159*radius*radius;}
float Sphere::volume()const{return 4*3.14159*radius*radius*radius/3;}
istream& operator >> (istream& input,Sphere& s){
input>>s.X>>s.Y>>s.radius;
return input;
}
ostream& operator << (ostream& output,Sphere& s){
output<<"["<<s.X<<","<<s.Y<<"],r="<<s.radius;
return output;
}
class Circle:public Point{ //圆
protected:
float radius;
public:
Circle();
Circle(float x,float y,float r):Point(x,y),radius(r){}
~Circle();
void setRadius(float);
float getRadius()const;
virtual float area()const;
};
Circle::Circle(){
X=0.0;
Y=0.0;
radius=0.0;
}
Circle::~Circle(){}
void Circle::setRadius(float r){radius=r;}
float Circle::getRadius()const{return radius;}
float Circle::area()const{return 3.14159*radius*radius;}
class Cylinder:public Circle{ //圆柱体
protected:
float height;
public:
Cylinder();
Cylinder(float x,float y,float r,float h):Circle(x,y,r),height(h){}
~Cylinder();
void setHeight(float);
virtual float area()const;
virtual float volume()const;
friend istream& operator >> (istream&,Cylinder&);
friend ostream& operator << (ostream&,Cylinder&);
};
Cylinder::Cylinder(){
X=0.0;
Y=0.0;
radius=0.0;
height=0.0;
}
Cylinder::~Cylinder(){}
void Cylinder::setHeight(float h){height=h;}
float Cylinder::area()const{return 2*Circle::area()+2*3.14159*radius*height;}
float Cylinder::volume()const{return Circle::area()*height;}
istream& operator >> (istream& input,Cylinder& c){
input>>c.X>>c.Y>>c.radius>>c.height;
return input;
}
ostream& operator << (ostream& output,Cylinder& c){
output<<"["<<c.X<<","<<c.Y<<"],r="<<c.radius<<",h="<<c.height;
return output;
}
int main(){
float a,v;
Cube cube;
Sphere sphere;
Cylinder cylinder;
cout<<"请输入正方体顶点坐标,边长:";
cin>>cube;
cout<<cube<<endl;
a=cube.area();
v=cube.volume();
cout<<"area:"<<a<<"\nvolume:"<<v<<endl;
cout<<"请输入球体顶点坐标,半径:";
cin>>sphere;
cout<<sphere<<endl;
a=sphere.area();
v=sphere.volume();
cout<<"area:"<<a<<"\nvolume:"<<v<<endl;
cout<<"请输入圆柱体顶点坐标,半径,高:";
cin>>cylinder;
cout<<cylinder<<endl;
a=cylinder.area();
v=cylinder.volume();
cout<<"area:"<<a<<"\nvolume:"<<v<<endl;
system ("PAUSE");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询