实验内容(学校练习)
程序1.编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。
程序2.设计一个楼房基类building,属性有floors(表示楼房层数)、areas(表示建筑面积)和name(表示建筑名称)。建立派生类house表示居住楼,增加成员ds表示单元数。建立派生类office表示办公楼,增加成员cs表示公司数。为每个类定义一个输出函数print,输出该类的相关信息。
程序3.定义点类,然后由点类生成圆类,再由圆类生成圆柱体类。圆类的属性有圆心、半径;方法有构造函数、析构函数、显示、计算面积。圆柱体类的新增属性有高度;方法有构造函数、析构函数、显示、计算体积。类的应用:创建一个圆柱体对象,显示其信息,求它的体积。
程序4.设计以下三个类:显卡类、主板类、集成了显卡的集成主板类。对这三个类的描述如下:
- 显卡类:CPU频率、显存频率。
- 主板类:北桥芯片类型、版型。
- 集成主板类:CPU频率、显存频率、北桥芯片类型、版型。
每个类都有自己的构造函数和析构函数,构造函数通过给定的初始值生成对应类的对象。同时这三个类都能显示其数据成员的值。
要求:通过类的多继承完成集成主板类的设计。
问题分析(问题的解决思路、算法分析等)
程序实现(完整程序代码)
调试与运行(运行结果的截图)
实验总结
-------------------------------------------------------------------------------------------------------------------------------
程序一代码如下
//#include<iostream> //#include<cstring> //#include<string> //using namespace std; // //class Person // { // public: // Person(string name1, int number1)//基类构造函数 // { // name = name1; // number = number1; // } // void display()//对基类数据的输入 // { // cout << "姓名:" << name << endl; // cout << "编号:" << number << endl; // } // protected://保护部分 // string name; // int number; // }; // //class Student :public Person //声明student类,是person的的派生 // { // public: // Student(string name1, int number1, int class_number1, int score1) :Person(name1, number1) //构造函数 // { // class_number = class_number1;//对新增数据初始化 班号、成绩 // score = score1;//成绩 // } // void show() // { // display();//输出姓名和编号 // cout << "班号:" << class_number << "\n" << "成绩:" << score << endl; // } // protected://保护成分 // int class_number; // int score; // }; // // class Teacher :public Person //派生类teacher // { // public: // Teacher(string name1, int number1, string profession1, string dept1) :Person(name1, number1) // { // profession = profession1; //对新增的数据成员初始化 // dept = dept1; // } // void show_all() // { // display(); // cout << "职称:" << profession << "\n" << "部门:" << dept << endl; // } // protected: // string profession; // string dept; // }; // // int main() // { // Student my_Student("羌族小煞", 3, 1141, 95); // cout << "学生:" << endl; // my_Student.show(); // // Teacher my_Teacher("胡老师", 01, "博士", "计算机部"); // cout << "教师:" << endl; // my_Teacher.show_all(); // // system("pause"); // return 0; // }程序二代码如下
//#include <iostream> //#include <cstring> //#include <string> //using namespace std; // //class Building //{ //public: // Building(int floors1,string name1,double area1) // { // floors=floors1; // name=name1; // area=area1; // } // void print() // { // cout<<"楼层:"<<floors<<endl; // cout<<"建筑名称:"<<name<<endl; // cout<<"面积:"<<area<<endl; // } //protected://保护部分 // int floors; // string name; // double area; //}; // //class House:public Building //{ // //public: // House(int floors1,string name1,double area1,int ds1):Building(floors1,name1,area1) // { // ds=ds1; // } // void print_0(); //protected: // int ds; //}; // //void House::print_0() //{ // cout<<"单元号:"<<ds<<endl; //} // // //class Office:public Building //{ //public: //Office(int floors1,string name1,double area1,int cs1):Building(floors1,name1,area1) // { // cs=cs1; // } //void print_1(); //protected: // int cs; //公司数 //}; // //void Office::print_1() //{ // cout<<"公司数为:"<<cs<<endl; //} // //int main() //{ // House A(10,"happy",40.0,4); // A.House::print_0(); // A.Building::print(); // cout<<endl; // // Office B(9,"csssss",80.0,20); // B.Office::print_1(); // B.Building::print(); // // system("pause"); // return 0;程序三代码如下
//#include <iostream> //#include<string> //#include<cstring> //using namespace std; // //class Point //{ //public: // Point(double x = 0, double y = 0); //构造函数 // void show1(); //显示 // ~Point(); //析构 //protected: //受保护成员 // double x, y; //}; // //Point::Point(double a, double b) //{ // x = a; // y = b; //} //Point::~Point(){} //void Point::show1() //{ // cout << "x=" << x << "\n" << "y=" << y << endl; //} // ////圆类是点类的派生类 //class Circle :public Point //{ //public: // Circle(double x = 0, double y = 0, double r = 0); //构造函数 // void show2(); //显示圆的半径 // double area()const; //计算圆面积 // ~Circle(); //析构 //protected: // double radius; //}; // // //Circle::Circle(double a, double b, double r) :Point(a, b), radius(r) {} //对圆心坐标和半径初始化 //void Circle::show2() //{ // cout << "r=" << radius << endl; //} //Circle::~Circle(){} //double Circle::area() const //{ // return 3.14159 * radius * radius; //} // ////圆柱类 //class Cylinder :public Circle //{ //public: // Cylinder(double x = 0, double y = 0, double r = 0, double h = 0);//构造函数 // void show3(); // double area() const; //计算圆表面积 S // double volume() const; //计算圆柱体积 V=S*h; // //protected: // double height; //圆柱高 //}; // ////定义构造函数 //Cylinder::Cylinder(double a, double b, double r, double h) :Circle(a, b, r), height(h) {} //void Cylinder::show3() //{ // cout << "h=" << height << endl; //} //double Cylinder::area() const //{ // return 2 * Circle::area() + 2 * 3.14159 * radius * height; //} //double Cylinder::volume() const //{ // return Circle::area() * height; //} // //int main() //{ // Cylinder cy1(3.5, 6.4, 1.0, 10); // cy1.show1(); // cy1.show2(); // cy1.show3(); // double V; // V = cy1.volume(); // cout << "圆柱的体积为" << V<<endl; // // system("pause"); // return 0;程序四代码如下
//#include<iostream> //#include<string> //#include<cstring> //#include <stdlib.h> //using namespace std; // //class Xianka //{ // public: // Xianka(double cpu1, double f11); // ~Xianka(); // void show1(); // // protected: // double cpu; //cpu频率 // double f1; //频率 //}; // //Xianka::Xianka(double cpu1, double f11) //{ // cpu = cpu1; // f1 = f11; //} //Xianka::~Xianka(){} //void Xianka::show1() //{ // cout << "显卡的cpu频率为" << cpu << "\n显卡的显存频率为" << f1 << endl; //} // //class Zhuban //{ // public: // Zhuban(double xp1, double bx1) // { // xp = xp1; // bx = bx1; // } // ~Zhuban(){} // void show2(); //protected: // double xp; // 北桥芯片类型 // double bx; //版型 //}; // //void Zhuban::show2() //{ // cout << "北桥芯片类型为" << xp << "\n版型为" << bx << endl; //} // // // //class Jicheng :public Xianka,public Zhuban //{ //public: // Jicheng(double cpu1,double f11,double xp1,double bx1):Xianka(cpu1,f11),Zhuban(xp1,bx1){} // ~Jicheng(){} // void show3() // { // cout<<"集显的cpu频率为" <<cpu << "显存频率为"<<f1 << "北桥芯片类型为" << xp << "\n版型为" << bx << endl; // } //}; // //int main() //{ // //Xianka a(88.8, 11.1); // //Zhuban b(1, 10.0); // //Jicheng c(); // //c.show3(); // // // //通过集成显卡对象调用父类的show() // Xianka* p = new Jicheng(88.8,1.1,1.0,10.0); // p->show1(); // delete p; // 释放 // p = NULL; // // //Zhuban* q = new Jicheng(88.8, 1.1, 1.0, 10.0); // //q->show2(); // //delete q; // 释放 // //q = NULL; // // // return 0; //}