两个C++的小题目
1.编写一个程序设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的私有派生类其中包含载人数passenger_loa...
1.编写一个程序设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的私有派生类其中包含载人数passenger_load。卡车类truck是vehicle的私有派生类其中包含载人数passenger_load和载重量payload,每个类都有相关数据的输出方法。
2.设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,它是从前两个类派生的,要求输出一个圆桌的高度、面积和颜色等数据。 展开
2.设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,它是从前两个类派生的,要求输出一个圆桌的高度、面积和颜色等数据。 展开
3个回答
展开全部
第一题:
#include <iostream>
using namespace std;
class vehicle
{
public:
//vehicle(){}
vehicle(int wh,double wei)
{
wheels = wh;
weight = wei;
}
void show()
{
cout<<"wheels= "<<wheels<<",weight= "<<weight<<endl;
}
private:
int wheels;
double weight;
};
class car: private vehicle
{
public:
car(int wh,double wei,int pass):vehicle(wh,wei)
{
//vehicle(wh,wei);
passenger_load = pass;
}
void show()
{
vehicle::show();
cout<<"passenger_load= "<<passenger_load<<endl;
}
private:
int passenger_load;
};
class truck:private vehicle
{
public:
truck(int wh,double wei,int pass,double payL):vehicle(wh,wei)
{
//vehicle(wh,wei);
passenger_load = pass;
payload = payL;
}
void show()
{
vehicle::show();
cout<<"passenger_load = "<<passenger_load
<<",payload = "<<payload<<endl;
}
private:
int passenger_load;
double payload;
};
void main()
{
vehicle v(4,6);
v.show();
car c(4,4,6);
c.show();
truck t(6,8,2,6);
t.show();
}
第二题:
#include <string>
#include <iostream>
using namespace std;
class Circle
{
public:
Circle(double r){radius=r;}
double Area(){return 3.14*radius*radius;}
private:
double radius;
};
class Table
{
public:
Table(double h,string col){height=h;color=col;}
double GetH(){return height;}
string GetCol(){return color;}
private:
double height;
string color;
};
class roundTable:public Circle,Table
{
public:
roundTable(double r,double h,string col):Circle(r),Table(h,col){}
void Display();
};
void roundTable::Display()
{
cout<<"Height of the roundTable is: "<<GetH()<<endl;
cout<<"Area of the roundTable is: "<<Area()<<endl;
cout<<"Color of the roundTable is: "<<GetCol()<<endl;
}
void main()
{
roundTable rt(1,1,"red");
rt.Display();
}
#include <iostream>
using namespace std;
class vehicle
{
public:
//vehicle(){}
vehicle(int wh,double wei)
{
wheels = wh;
weight = wei;
}
void show()
{
cout<<"wheels= "<<wheels<<",weight= "<<weight<<endl;
}
private:
int wheels;
double weight;
};
class car: private vehicle
{
public:
car(int wh,double wei,int pass):vehicle(wh,wei)
{
//vehicle(wh,wei);
passenger_load = pass;
}
void show()
{
vehicle::show();
cout<<"passenger_load= "<<passenger_load<<endl;
}
private:
int passenger_load;
};
class truck:private vehicle
{
public:
truck(int wh,double wei,int pass,double payL):vehicle(wh,wei)
{
//vehicle(wh,wei);
passenger_load = pass;
payload = payL;
}
void show()
{
vehicle::show();
cout<<"passenger_load = "<<passenger_load
<<",payload = "<<payload<<endl;
}
private:
int passenger_load;
double payload;
};
void main()
{
vehicle v(4,6);
v.show();
car c(4,4,6);
c.show();
truck t(6,8,2,6);
t.show();
}
第二题:
#include <string>
#include <iostream>
using namespace std;
class Circle
{
public:
Circle(double r){radius=r;}
double Area(){return 3.14*radius*radius;}
private:
double radius;
};
class Table
{
public:
Table(double h,string col){height=h;color=col;}
double GetH(){return height;}
string GetCol(){return color;}
private:
double height;
string color;
};
class roundTable:public Circle,Table
{
public:
roundTable(double r,double h,string col):Circle(r),Table(h,col){}
void Display();
};
void roundTable::Display()
{
cout<<"Height of the roundTable is: "<<GetH()<<endl;
cout<<"Area of the roundTable is: "<<Area()<<endl;
cout<<"Color of the roundTable is: "<<GetCol()<<endl;
}
void main()
{
roundTable rt(1,1,"red");
rt.Display();
}
展开全部
第一个:
#include<iostream>
using namespace std;
class vehicle{
protected:
int wheels;
float weight;
public:
void display();
};
class car:private vehicle{
private:
int passenger_load;
public:
void display();
};
class truck:private vehicle{
private:
int passenger_load;
float payload;
public:
void display();
};
void vehicle::display(){
cout<<"wheels are :"<<wheels<<endl;
cout<<"weight is :"<<weight<<"kg"<<endl;
}
void car::display(){
cout<<"the car's wheels are :"<<wheels<<endl;
cout<<"weight is :"<<weight<<"kg"<<endl;
cout<<"max passenger load are :"<<passenger_load<<endl;
}
void truck::display(){
cout<<"the truck's wheels are :"<<wheels<<endl;
cout<<"weight is :"<<weight<<"kg"<<endl;
cout<<"max passenger load are :"<<passenger_load<<endl;
cout<<"pay load is"<<payload<<endl;
}
void main(){
}
好像第二个也差不多,不想写了。
#include<iostream>
using namespace std;
class vehicle{
protected:
int wheels;
float weight;
public:
void display();
};
class car:private vehicle{
private:
int passenger_load;
public:
void display();
};
class truck:private vehicle{
private:
int passenger_load;
float payload;
public:
void display();
};
void vehicle::display(){
cout<<"wheels are :"<<wheels<<endl;
cout<<"weight is :"<<weight<<"kg"<<endl;
}
void car::display(){
cout<<"the car's wheels are :"<<wheels<<endl;
cout<<"weight is :"<<weight<<"kg"<<endl;
cout<<"max passenger load are :"<<passenger_load<<endl;
}
void truck::display(){
cout<<"the truck's wheels are :"<<wheels<<endl;
cout<<"weight is :"<<weight<<"kg"<<endl;
cout<<"max passenger load are :"<<passenger_load<<endl;
cout<<"pay load is"<<payload<<endl;
}
void main(){
}
好像第二个也差不多,不想写了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第一题:
class vehicle
{
private:
int wheels;
float weight;
vehicle(0,0.0);
int get_wheels();
float get_weight();
void printV();
};
class car:private vehicle
{
private:
int passenger_load;
public:
car(4);
int get_passengers();
void printC();
}
class truck:private vehicle
{
private:
int passenger_load;
float payload;
public:
truck(0,0.0);
int get_passengers();
float get_payload();
void printT();
}
第二题:
class circle
{
float r;
void area(float R);
void Print();
}
class table
{
float high;
char color;
void print();
}
class roundtable : public circle,table
{
}
期中内容你可以自己加,我给的是框架;函数内容自己写。。。。
class vehicle
{
private:
int wheels;
float weight;
vehicle(0,0.0);
int get_wheels();
float get_weight();
void printV();
};
class car:private vehicle
{
private:
int passenger_load;
public:
car(4);
int get_passengers();
void printC();
}
class truck:private vehicle
{
private:
int passenger_load;
float payload;
public:
truck(0,0.0);
int get_passengers();
float get_payload();
void printT();
}
第二题:
class circle
{
float r;
void area(float R);
void Print();
}
class table
{
float high;
char color;
void print();
}
class roundtable : public circle,table
{
}
期中内容你可以自己加,我给的是框架;函数内容自己写。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询