两个C++的编程问题,求各位大虾帮帮手~要求写出完整并能运行的程序代码,谢谢各位帮我呀~~~很急的呀

1、编写一个程序,定义一个汽车类vehicle,它具有一个需传递参数的构造函数,类中的数据成员包括车轮个数和车的重量,并放到保护段中;定义轿车类car是汽车类vehicle的私有派生类,其中包含载人数;再定义卡车类truck是汽车类vehicle的私有派生类,其中包含载人数和载重量。每个类都有相应的数据输出。
2、设有系类,它的数据包括系编号、系名,功能包括置数据、修改数据和取数据。设有班类,它继承系类,另数据包括学号、姓名、性别,功能包括置数据、修改数据和取数据。主函数默认设置一位同学数据,键盘输入一位同学数据,然后显示二位同学数据。

这是第一个提的头文件 vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H

#include <iostream>

class Vehicle {
public:
Vehicle():numWheel(0), weight(0) {}
Vehicle(unsigned int nw, double wt):
numWheel(nw), weight(wt) {}

friend std::ostream& operator << (std::ostream& out, const Vehicle& veh);
protected:
unsigned int numWheel;
double weight;
};

class Car : private Vehicle {
public:
Car():Vehicle(), numPep(0) {}
Car(unsigned int nw, double wt, unsigned int np):
Vehicle(), numPep(np) {}

friend std::ostream& operator << (std::ostream& out, const Car& car);
private:
unsigned int numPep;
};

class Truck : private Vehicle {
public:
Truck():Vehicle(), numPep(0), load(0) {}
Truck(unsigned int nw, double wt, unsigned int np,
double ld):
Vehicle(), numPep(np), load(ld) {}

friend std::ostream& operator << (std::ostream& out, const Truck& truck);
private:
unsigned int numPep;
double load;
};

std::ostream& operator << (std::ostream& out, const Vehicle& veh)
{
out << "Vehicle: " << std::endl
<< "number of wheel: " << veh.numWheel
<< ", weight: " << veh.weight << std::endl;
return out;
}

std::ostream& operator << (std::ostream& out, const Car& car)
{
out << "Car: " << std::endl
<< "number of wheel: " << car.numWheel
<< ", weight: " << car.weight
<< ", man can load: " << car.numPep
<< std::endl;
return out;
}

std::ostream& operator << (std::ostream& out, const Truck& truck)
{
out << "Truck: " << std::endl
<< "number of wheel: " << truck.numWheel
<< ", weight: " << truck.weight
<< ", man can load: " << truck.numPep
<< ", can load: " << truck.load
<< std::endl;
return out;
}

#endif // VEHICLE_H

这是第二个题的头文件department.h
#ifndef DEPARTMENT_H
#define DEPARTMENT_H

#include <iostream>
#include <string>
using std::string;

class Department {
public:
Department():numD(""), nameD("") {}
Department(string numd, string namd):
numD(numd), nameD(namd) {}

void setNumd(string&);
string getNumd() const;
void setNamed(string&);
string getNamed() const;
virtual void print() const;
private:
string numD;
string nameD;
};

class Class : public Department {
public:
Class():Department(), numC(""), nameC("") {}
Class(string numd, string named, string numc,
string namec):
Department(numd, named), numC(numc), nameC(namec) {}

void setNumc(string&);
string getNumc() const;
void setNamec(string&);
string getNamec() const;
void print() const;
private:
string numC;
string nameC;
};

class Student:public Class {
public:
Student():Class(), numS(""), nameS(""), genderS("mail") {}
Student(string numd, string named, string numc,
string namec, string nums, string names,
string genders):
Class(numd, named, numc,
namec), numS(nums), nameS(names), genderS(genders) {}

void setNums(string&);
string getNums() const;
void setNames(string&);
string getNames() const;
void setGenders(string&);
string getGenders() const;
void print() const;
private:
string numS;
string nameS;
string genderS;
};

inline void Department::setNumd(string& numd)
{
numD = numd;
}

inline string Department::getNumd() const
{
return numD;
}

inline void Department::setNamed(string& named)
{
nameD = named;
}

inline string Department::getNamed() const
{
return nameD;
}

inline void Department::print() const
{
std::cout << "the number of Department: "
<< numD << std::endl
<< "the name of Department: "
<< nameD << std::endl;
}

inline void Class::setNumc(string& numc)
{
numC = numc;
}

inline string Class::getNumc() const
{
return numC;
}

inline void Class::setNamec(string& namec)
{
nameC = namec;
}

inline string Class::getNamec() const
{
return nameC;
}

inline void Class::print() const
{
std::cout << "the number of Class: "
<< getNumd() << numC << std::endl
<< "the name of Class: "
<< getNamed() << " " << nameC << std::endl;
}

inline void Student::setNums(string& nums)
{
numS = nums;
}

inline string Student::getNums() const
{
return numS;
}

inline void Student::setNames(string& names)
{
nameS = names;
}

inline string Student::getNames() const
{
return nameS;
}

inline void Student::setGenders(string& genders)
{
genderS = genders;
}

inline string Student::getGenders() const
{
return genderS;
}

inline void Student::print() const
{
std::cout << "the number of Student: "
<< getNumd() << getNumc() << numS << std::endl
<< "the name of Student: "
<< nameS << std::endl
<< "the gender of Student: "
<< genderS << std::endl
<< "the student in: "
<< getNamed() <<" " << getNamec() << std::endl;
}
#endif // DEPARTMENT_H
由于你题目上描述的不是很清楚,置数据,和修改数据有区别吗?我都弄了一个函数set...(type);
为了方便起见,我没有将累的实现放在.cpp中而是和类的定义放在了一块:
第二个题目的主函数:
#include <iostream>

#include "department.h"

using namespace std;

int main ()
{
Department computer("1000", "Computer");
computer.print();
cout << endl;
Class software("1000", "Computer", "01", "Software");
software.print();
cout << endl;
Student yang("1000", "Computer", "01", "Software", "20", "yang ning", "male");
yang.print();
}
这里我没有输入令一个同学,你自己编吧,很容易的,你可以重载>>运算符,也可以用set...(type)一个一个来输入,你还可以改的更全面,还有第一个题的主函数,你也可以编一个验证一下,我都验证过了!
我已经很累了,先睡了!这已经是我在baidu知道上回答最长的问题了,可能不全,请见谅!
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-05-14
第二个问题的答案。看是否正确,如有修改,再联系

#include<iostream>
#include<string>
using namespace std;
//定义系类Xi
class Xi
{
public:
void setdata(string s,int i) //设置数据
{
xi_name=s;
xi_num=i;
}
void revisedata(string s,int i) //修改数据
{
xi_name=s;
xi_num=i;
}
void getdata(string s,int i) //读取数据
{
xi_name=s;
xi_num=i;
}
public:
string xi_name; //系名
int xi_num; //系编号
};
//派生班类Ban
class Ban:public Xi
{
public:
void setdata(string x,string y,int i)
{
num=i;
name=x;
sex=y;
}
void revisedata(string x,string y,int i)
{
num=i;
name=x;
sex=y;
}
void getdata(string x,string y,int i)
{
num=i;
name=x;
sex=y;
}
void show() //显示数据
{
cout<<"编号: "<<xi_num<<endl;
cout<<"系名: "<<xi_name<<endl;
cout<<"学号: "<<num<<endl;
cout<<"姓名: "<<name<<endl;
cout<<"性别: "<<sex<<endl;
}
public:
int num; //学号
string name; //姓名
string sex; //性别
};
//主函数
void main()
{
Ban b1; //主函数中有默认值
Ban b2; //手工录入
b1.Xi::setdata("英语",1001);
b1.setdata("张三","男",9527);
//录入第二名同学的信息
cout<<"请输入第二名同学的信息"<<endl;
cout<<"编号:";
cin>>b2.Xi::xi_num;
cout<<"系名:";
cin>>b2.Xi::xi_name;
cout<<"学号:";
cin>>b2.num;
cout<<"姓名:";
cin>>b2.name;
cout<<"性别:";
cin>>b2.sex;
cout<<endl<<"主函数自己生成的同学信息:"<<endl;
b1.show();
cout<<endl<<"录入的第二名同学的信息:"<<endl;
b2.show();
}
第2个回答  2009-05-14
帮你看一下
相似回答