| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 778 人关注过本帖
标题:求编程牛人帮忙编2个C++程序
只看楼主 加入收藏
baojin
Rank: 2
等 级:论坛游民
帖 子:28
专家分:73
注 册:2009-10-12
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:4 
求编程牛人帮忙编2个C++程序
(a)   请你编写一个程序,设计一个汽车类,包含的数据成员有:车轮个数和车重。小车类car是它的私有派生类,其中包含载人数。卡车类是汽车类的私有派生类,其中包含载人数和载重量,每个类都有相关数据的输出方法。
(b)    设计一个基类,包括编号、姓名、住址及相关的成员函数,由它派生出学生类和教师类,并用一些数据进行测试。
要求:使用继承类与蕨类构造函数和析构函数
      使用继承成员访问控制的运用
搜索更多相关主题的帖子: 汽车 姓名 
2009-10-12 14:11
cneagle
Rank: 1
等 级:新手上路
威 望:1
帖 子:10
专家分:4
注 册:2009-10-12
收藏
得分:0 
upup

为生存!
2009-10-12 16:20
哥特复兴
Rank: 2
来 自:China-上海
等 级:论坛游民
帖 子:20
专家分:64
注 册:2009-6-23
收藏
得分:0 
为什么都是私有继承?
私有继承没有意义。因为是私有继承,所以所有数据成员为受保护类型,否则程序无法运行。
代码如下:
#include<iostream>
using   namespace std;
class Automobile              //汽车类
{
protected:
   int wheel;   //车轮
   double wieght;   //车重
public:
    Automobile(int wheel=0,double wieght=0)
    {
        this->wheel=wheel;
        this->wieght=wieght;
    }
    void show()
    {  
        cout<<"wheel:"<<wheel<<endl;
        cout<<"wieght:"<<wieght<<endl;;
        cout<<endl;
    }
};
class car : private Automobile  //小汽车
{
protected:
    int seat;        //座位
public:
    car(int seat=0) : Automobile(wheel,wieght)
    {
        this->seat=seat;
    }
    void show()
    {
        cout<<"seat:"<<seat<<endl;
        cout<<endl;
    }
};
class StationWagon : private Automobile
{
protected:
    int seat;    //座位
    double load;  //载重
public:
    StationWagon(int seat,double load) : Automobile(wheel,wieght)
    {
        this->seat=seat;
        this->load=load;
    }
    void show()
    {
        cout<<"seat:"<<seat<<endl;
        cout<<"load:"<<load<<endl;
    }
};
void main()
{
    Automobile c1(4,10);     //创建对象并初始化
    c1.show();
 
    car c2(30);           //创建对象并初始化
    c2.show();
 
    StationWagon c3(50,100);         //创建对象并初始化
    c3.show();
 
}
2009-10-12 17:02
哥特复兴
Rank: 2
来 自:China-上海
等 级:论坛游民
帖 子:20
专家分:64
注 册:2009-6-23
收藏
得分:0 
吃饭了,
第二题和第一题差不多,自己试着写吧。
2009-10-12 17:05
哥特复兴
Rank: 2
来 自:China-上海
等 级:论坛游民
帖 子:20
专家分:64
注 册:2009-6-23
收藏
得分:20 
(A)
#include<iostream>  
using   namespace std;  
class Automobile              //汽车类  
{  
protected:  
   int wheel;                     //保护数据成员车轮  
   double wieght;                 //保护数据成员车重  
public:  
    Automobile(int wheel=0,double wieght=0)   //构造函数
    {  
        this->wheel=wheel;  
        this->wieght=wieght;  
    }  
    ~Automobile()                            //析构函数
    {
    }
    void show()                                //成员函数
    {   
        cout<<"wheel:"<<wheel<<endl;  
        cout<<"wieght:"<<wieght<<endl;;  
        cout<<endl;  
    }  
};  
class car : private Automobile             //小汽车类私有继承汽车类  
{  
protected:  
    int seat;                            //保护数据成员座位  
public:  
    car(int seat=0) : Automobile(wheel,wieght)                   //构造函数
    {  
        this->seat=seat;  
    }  
    ~car()                                           //析构函数
    {
    }
    void show()                                       //成员函数
    {  
        cout<<"seat:"<<seat<<endl;  
        cout<<endl;  
    }  
};  
class StationWagon : private Automobile         //客货两用车类,私有继承汽车类
{  
protected:   
    int seat;                          //保护数据成员座位  
    double load;                       //保护数据成员载重  
public:  
    StationWagon(int seat,double load) : Automobile(wheel,wieght)                  //构造函数
    {  
        this->seat=seat;  
        this->load=load;  
    }  
    ~StationWagon()                              //析构函数
    {
    }
    void show()                                  //成员函数
    {  
        cout<<"seat:"<<seat<<endl;  
        cout<<"load:"<<load<<endl;  
    }  
};  
void main()  
{  
    Automobile c1(4,10);     //创建对象并初始化  
    Automobile *p;
    p=&c1;
    p->show();  
 
    car c2(30);           //创建对象并初始化  
    car *p1;
    p1=&c2;
    p1->show();  
 
    StationWagon c3(50,100);         //创建对象并初始化  
    StationWagon *p2;
    p2=&c3;
    p2->show();  
}
(B)
#include<iostream>
#include<string>                //字符串头文件
using namespace std;
class person
{
protected:
    int number;             //编号
    bool sex;                    //性别
    string name;             //姓名
    string address;           //地址
public:
    person(int number,bool sex,string name,string address)                //构造函数
    {
        this->number=number;
        this->sex=sex;
        this->name=name;
        this->address=address;
    }
    ~person()                                                         //析构函数
    {
    }
    void show()                                                 //成员函数
    {
        cout<<"编号:"<<number<<endl;
        cout<<"姓名:"<<name<<endl;
        cout<<"性别:"<<sex<<endl;
        cout<<"住址:"<<address<<endl;
    }
};
class teacher : public person                   //创建一教师类,公有继承人类
{
protected:
    int grade;                        //级别
    double salary;                         //月薪
public:
    teacher(int grade,double salary) : person(number,sex,name,address)         //构造函数
    {
        this->grade=grade;
        this->salary=salary;
    }
    ~teacher()
    {
    }
    void show()                 //计算教师实际工资函数
    {
        double Factsalary;                 //创建一变量用来存放实际工资
        Factsalary=grade*salary;            //实际工资=级别*月薪     
        cout<<"该教师的实际工资为:"<<Factsalary<<endl;
    }
};
class student : public person
{
protected:
    int mark;              //学分
    int grade;             //年级级别
public:
    student(int mark,int grade) : person(number,sex,name,address)      //构造函数
    {
        this->mark=mark;
        this->grade=grade;
    }
    ~student()                                //析构函数
    {
    }
    void show()                        //计算总学分函数
    {
    int Nummark;         
    Nummark=mark+grade;                     //总学分=学分+年级级别
    cout<<"该学生的总学分为:"<<Nummark<<endl;
    }
 
};
void main()
{
    person c1(54,0,"liunan","dkjflkdjf");    //创建函数对象并初始化
    c1.show();
 
    teacher c2(3,2000);               //创建函数对象并初始化         
    c2.show();
 
    student c3(10,3);                   //创建函数对象并初始化
    c3.show();
}
2009-10-13 15:37
快速回复:求编程牛人帮忙编2个C++程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025354 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved