两道C++题目,高手请进,速度来哦!答得好的绝对加分!

1.声明Book与Ruler两个类,二者都有weight属性,定义二者的一个友元函数totalWeight(),计算二者的重量和.
2。利用继承性与派生类来管理学生和教师档案.假设要管理下述几类人员的如下一些数据.
teacher(教师)类:姓名,性别,年龄,职称,担任课程;
student(学生)类:姓名,性别,年龄,学号,系别;
gradstudent(研究生)类:姓名,性别,年龄,学号,系别,导师。
要求每个类只设立构造函数以及显示类的对象数据的成员函数。编写主函数,说明有关类对象,并对其类成员函数进行简单使用。

第一个题目本人觉得使用友元类做思路更清晰一些,一个类是另一个类的友元类则,这个类的所有函数都是另一个类的友元函数,当然方法不是唯一的~
参考如下:
#include<iostream>
using namespace std;

class Book
{
private:
double weight;
public:
Book(){}
Book(double weight)
{
this->weight = weight;
}

friend class calculate;//сят╙юЮ
};

class Ruler
{
private:
double weight;
public:
Ruler(){}
Ruler(double weight)
{
this->weight = weight;
}

friend class calculate;
};

class calculate
{
private:
Book b;
Ruler r;
double totalweight;
public:
calculate(Book b,Ruler r)
{
this ->b = b;
this ->r = r;
}
void show()
{
cout<<"the total weight is :"<<totalweight<<endl;
}

void totalWeight()
{
totalweight = b.weight + r.weight;
}
};

int main()
{
Book b(2.1);
Ruler r(3.2);
calculate c(b,r);
c.totalWeight();
c.show();
}

如果不适用友元类改为如下:
#include<iostream>
using namespace std;

class Book
{
public:
double weight;
public:
Book(){}
Book(double weight)
{
this->weight = weight;
}

// friend class calculate;//友元类
};

class Ruler
{
public:
double weight;
public:
Ruler(){}
Ruler(double weight)
{
this->weight = weight;
}

// friend class calculate;
};

class calculate
{
private:
Book b;
Ruler r;
double totalweight;
public:
calculate(){};
calculate(Book b,Ruler r)
{
this ->b = b;
this ->r = r;
}
void show()
{
cout<<"the total weight is :"<<totalweight<<endl;
}

friend void totalWeight(calculate &c)
{
c.totalweight = c.b.weight + c.r.weight;
}
};

int main()
{
Book b(2.1);
Ruler r(3.2);
calculate c(b,r);
totalWeight(c);
c.show();
return 0;
}

第二个题目大体写了一下,你可以再改改,仅供参考:
#include <iostream>
#include<string>
using namespace std;

class people
{
protected:
string name;
int age;
string sex;
public:
people(string name,int age,string sex)
{
this->name = name;
this->age = age;
this->sex = sex;
}

};

class student:public people
{
private:
string department;
string id;
public:
student(string name, int age,string sex,string department,string id):people(name,age,sex)
{
this->department = department;
this->id = id;

}

void show()
{
cout<<name<<"\t"<<age<<"\t"<<sex<<"\t"<<department<<"\t"<<id<<endl;
}
};

class teacher:public people
{
private:
string major;
string role;

public:
teacher(string name, int age,string sex,string major,string role):people(name,age,sex)
{
this->major =major;
this->role = role;

}

void show()
{
cout<< name<< "\t"<< age << "\t"<< sex << "\t" << major <<"\t"<< role <<endl;
}

};

class gradstudent:public people
{
private:
string department;
string tutor;
public:
gradstudent(string name, int age,string sex,string department,string tutor):people(name,age,sex)
{
this->department = department;
this->tutor = tutor;
}

void show()
{
cout<< name<< "\t"<< age << "\t"<< sex << "\t" << department <<"\t"<< tutor <<endl;
}
};

int main ()
{
student s("王明",20,"男","物理系","082060201");
teacher t("张三",45,"男","计算机专业","讲师");
gradstudent g("李四",26,"男","化学系","徐东");
s.show();
t.show();
g.show();
return 0;

}
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-04-29
/*
1.声明Book与Ruler两个类,二者都有weight属性,定义二者的
一个友元函数totalWeight(),计算二者的重量和.

2。利用继承性与派生类来管理学生和教师档案.假设要管理下述几类人员的如下一些数据.
teacher(教师)类:姓名,性别,年龄,职称,担任课程;
student(学生)类:姓名,性别,年龄,学号,系别;
gradstudent(研究生)类:姓名,性别,年龄,学号,系别,导师。
要求每个类只设立构造函数以及显示类的对象数据的成员函数。
编写主函数,说明有关类对象,并对其类成员函数进行简单使用。
*/

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

//因为Book中的友元函数要用到Ruler类,先声明但不定义
class Ruler;

class Book
{
public:
//构造函数
Book(double w = 10.0){weight = w;}
//友元函数
friend double totalWeight(class Book & b,class Ruler & r);
private:
double weight;
};
class Ruler
{
public:
Ruler(double w = 10.0){weight = w;}
//友元函数
friend double totalWeight(class Book & b,class Ruler & r);
private:
double weight;
};

double totalWeight(class Book & b,class Ruler & r)
{
//由于 totalWeight 是 Book 和 Ruler 类的友元函数
//因此可以直接引用它们的私有成员 weight
return b.weight + r.weight;
}

//student,teacher,gradstudent 类 的父类 Person 类
class Person
{
public:
//构造函数
Person(string n,string s,int a){name=n,sex=s,age=a;}
//显示类的对象数据的成员函数
void Display(){cout<<"name:"<<name<<endl<<"sex:"<<sex<<endl<<"age:"<<age<<endl;}
private:
string name;
string sex;
int age;
};

class Student: public Person
{
public:
Student(string n,string s,int a,string nu,string d):Person(n,s,a),number(nu),department(d){}
void Display(){Person::Display();cout<<"number:"<<number<<endl<<"department:"<<department<<endl<<endl;}
private:
string number;
string department;
};

class Teacher : public Person
{
public:
Teacher(string n,string s,int a,string t,string c):Person(n,s,a),title(t),course(c){}
void Display(){Person::Display();cout<<"title:"<<title<<endl<<"course"<<course<<endl<<endl;}
private:
string title;
string course;
};

class Gradstudent: public Person
{
public:
Gradstudent(string n,string s,int a,string nu,string d,string m):Person(n,s,a),number(nu),department(d),master(m){}
void Display(){Person::Display();cout<<"number:"<<number<<endl<<"department:"<<department<<endl<<"master:"<<master<<endl<<endl;}
private:
string number;
string department;
string master;
};

int main(int argc, char *argv[])
{
Book b;
Ruler r;
cout<<"the total weight of book and ruler is :"<<totalWeight(b,r)<<endl<<endl;

Student s("八戒","男",400,"003","高老庄");
Teacher t("唐僧","男",50,"长老","紧箍咒");
Gradstudent g("孙悟空","男",100,"001","花果山","唐僧");

s.Display();
t.Display();
g.Display();

return 0;
}本回答被提问者采纳
第2个回答  2010-04-29
额 我今天刚刚看了继承和派生
不过这个貌似比较麻烦....
相似回答