c++继承、派生
2017-03-09 20:17
239 查看
1、编写一个程序计算出球、圆柱和圆锥的表面积和体积。
要求:
(1)定义一个基类,至少含有一个数据成员半径,并设为保护成员;
(2)定义基类的派生类球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数;
(3)编写主函数,求球、圆柱、圆锥的表面积和体积。
2、编写一个学生和教师数据输入和显示程序。其中,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。
要求:
(1)将编号、姓名输入和显示设计成一个类person;
(2)设计类person的派生类:学生类student和教师类teacher;
(3)各个类的声明放在相应的头文件中(.h),类的实现放在相应的实现文件中(.cpp):person.h,person.cpp,student.h,student.cpp,teacher.h,teacher.cpp;
(4)编写一个主文件(SY4_2.cpp),在该文件中分别定义student、teacher的对象,完成相应功能。
这次遇见的主要问题就是派生函数在使用多文件结构的时候重复定义的问题,通过#pragrame once放在基类里面就可以避免这样的问题。
要求:
(1)定义一个基类,至少含有一个数据成员半径,并设为保护成员;
(2)定义基类的派生类球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数;
(3)编写主函数,求球、圆柱、圆锥的表面积和体积。
#include<iostream> using namespace std; #define PI 3.1415 class point{ protected: double r,h; }; class Ball: public point{ public: void set_Ball(){ cout<<"请输入球的半径:"; cin>>r; } void show_Ball(){ cout<<"球的表面积为:"<<4*PI*r*r<<endl; cout<<"球的体积为:"<<(4/3)*PI*r*r*r<<endl; cout<<endl; } }; class Column: public point{ public: void set_Column(){ cout<<"请输入圆柱底面半径: "; cin>>r; //cout<<endl; cout<<"请输入圆柱的高: "; cin>>h; } void show_Column(){ cout<<"圆柱体的面积为: "<<2*PI*r*r+2*PI*r*h<<endl; cout<<"圆柱体的体积为: "<<PI*r*r*h<<endl; cout<<endl; } }; class Cone: public point{ public: void set_Cone(){ cout<<"请输入圆锥底面半径: "; cin>>r; //cout<<endl; cout<<"请输入圆锥的高: "; cin>>h; } void show_Cone(){ cout<<"圆锥的面积为: "<<PI*r*h+PI*r*r<<endl; cout<<"圆锥的体积为: " 4000 <<PI*r*r*h/3<<endl; } }; int main(){ Ball base1; base1.set_Ball(); base1.show_Ball(); Column base2; base2.set_Column(); base2.show_Column(); Cone base3; base3.set_Cone(); base3.show_Cone(); return 0; }
2、编写一个学生和教师数据输入和显示程序。其中,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。
要求:
(1)将编号、姓名输入和显示设计成一个类person;
(2)设计类person的派生类:学生类student和教师类teacher;
(3)各个类的声明放在相应的头文件中(.h),类的实现放在相应的实现文件中(.cpp):person.h,person.cpp,student.h,student.cpp,teacher.h,teacher.cpp;
(4)编写一个主文件(SY4_2.cpp),在该文件中分别定义student、teacher的对象,完成相应功能。
//SY4_2.cpp #include <iostream> using namespace std; #include"teacher.h" #include"person.h" #include"student.h" /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { student base1; cout<<"请输入学生信息:"<<endl; base1.set_information(); base1.set_student(); cout<<endl; cout<<"学生信息为:"<<endl; base1.show_information(); base1.show_student(); cout<<endl; teacher base2; cout<<"请输入教师信息:"; base2.set_information(); base2.set_teacher(); cout<<endl; cout<<"教师信息为:"<<endl; base2.show_information(); base2.show_teacher(); cout<<endl; return 0; } //person.h #pragma once class person{ public: void set_information( ); void show_information( ); private: int number; char name[30]; }; //person.cpp #include <iostream> using namespace std; #include"person.h" void person::set_information( ) { cout<<"请输入基本信息: "<<endl; cout<<"姓名:"; cin>>name; cout<<"编号:"; cin>>number; } void person::show_information( ) { cout<<"姓名:"<<name<<endl; cout<<"编号:"<<number<<endl; } //student.h #include"person.h" class student: public person{ public: void set_student(); void show_student(); private: int Grade,Glass; }; //student.cpp #include <iostream> using namespace std; #include"student.h" void student::set_student(){ cout<<"请输入学生学号:"; cin>>Glass; cout<<"请输入学生成绩:"; cin>>Grade; } void student::show_student(){ // cout<<"学生信息为:"<<endl; cout<<"学生学号为:"<<Glass<<endl; cout<<"学生成绩为:"<<Grade<<endl; } //teacher.h #include"person.h" class teacher: public person{ public: void set_teacher(); void show_teacher(); private: char pro[10]; char dep[10]; }; //teacher.cpp #include <iostream> using namespace std; #include"teacher.h" void teacher::set_teacher(){ cout<<"请输入教师职称:"; cin>>pro; cout<<"请输入教师部门:"; cin>>dep; } void teacher::show_teacher(){ cout<<"教师信息为:"<<endl; cout<<"教师职称:"<<pro<<endl; cout<<"教师部门:"<<dep<<endl; }
这次遇见的主要问题就是派生函数在使用多文件结构的时候重复定义的问题,通过#pragrame once放在基类里面就可以避免这样的问题。
相关文章推荐
- C/C++基础知识总结——继承与派生
- C++第四次实验——派生和继承1
- C++学习笔记——继承与派生(二)
- C++学习摘要之三:继承和派生
- C++程序设计-第11周 继承与派生
- C++ 继承与派生
- C++继承与派生
- C++: 类的继承派生
- 【C++的探索路12】继承与派生之高级篇--派生类与赋值运算符及多重继承
- c++继承和派生的理解
- C++作业4-继承和派生1
- C++之继承与派生(1)
- c++ 类的继承与派生
- c++继承与派生
- C++继承与派生
- 关于C++程序设计的基础核心之二:继承与派生的基本知识
- 20170622_C++的继承与派生1
- C++继承与派生的访问基本规则
- C++第10/11周项目 继承与派生 【项目3 - 警察和厨师】(复习新版)
- C++入门进阶之5: 继承与派生