这是第一个提的头文件 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知道上回答最长的问题了,可能不全,请见谅!
温馨提示:内容为网友见解,仅供参考