当前位置:文档之家› 实验四参考答案

实验四参考答案

class A
{
protected:
char name[80];
public:
A(const char *n )
{
strcpy(name,n);
}
};
class B: public A
{
public:
B(_______________)______________
{}
void PrintName()
{
cout<<"name:"<<name<<endl;
}
};
intmain()
{
B b1("小明");
b1.PrintName() ;
return 0;
}
填入的内容
const char* n
:A(n)
2.
#include<iostream>
using namespace std;
class Base
cout<<"请输入专业:"; cin>>major;
}
void Student::output()
{
Person::output();
cout<<"学号:"<<stu_no;
cout<<"专业:"<<major;
}
Student::Student(string name,string sex,string id,string phone,string stu_no,string major)
using namespace std;
class Person
{
string name;
string sex;
string id;
string phone;
public:
void output();
void input();
Person(string name="",string sex="",string id="",string phone="");
}
};
int main()
{
Derived d;
d.fun();
}
填入的内容
public:
Base::fun();
2)编程:编程序并上机调试运行。
1.编写一个学生和教师数据输入和显示程序,学生数据有学号、姓名、性别、身份证号、联系电话、专业,教师的数据有工号、姓名、性别、身份证号、联系电话、职称、部门。要求编写三个类实现:
{
this->name=name;
this->sex=sex;
this->id=id;
this->phone=phone;
};
class Student:public Person
{
string stu_no;
string major;
public:
void input();
void output();
cout<<"请输入姓名:";cin>>name;
cout<<"请输入性别:";cin>>sex;
cout<<"请输入身份证号:";cin>>id;
cout<<"请输入电话:";cin>>phone;
}
Person::Person(string name,string sex,string id,string phone)
{
Person::input();
cout<<"请输入职工号:"; cin>>tea_no;
cout<<"请输入职称:"; cin>>prof;
cout<<"请输入所在部门:"; cin>>dept;
}
void Teacher::output()
{
Person::output();
cout<<"职工号:"<<tea_no;
:Person(name,sex,id,phone)
{
this->stu_no=stu_no;
this->major=major;
}
class Teacher:public Person
{
string tea_no;
string prof;
string dept;
public:
void input();
void output();
Teacher(string name="",string sex="",string id="",string phone="",string tea_no="",string prof="",string dept="");
};
void Teacher::input()
};
void Person::output()
{
cout<<"姓名:"<<name<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"身份证号:"<<id<<endl;
cout<<"电话号码:"<<phone<<endl;
}
void Person::input()
{
cout<<endl;
一、实验目的
掌握继承与派生的概念。
掌握派生类的声明与构成。
掌握派生类的继承方式和访问控制权限。
掌握派生类构造函数和析构函数的使用方法。
掌握基类与派生类的类型转换。
二、实验内容和步骤
1)程序填空:填入相应的内容,使得程序可以正常运行。
1.#include <iostreamout<<"职称:"<<prof;
cout<<"部门:"<<dept;
}
Teacher::Teacher(string name,string sex,string id,string phone,string tea_no,string prof,string dept)
Student(string name="",string sex="",string id="",string phone="",string stu_no="",string major="");
};
void Student::input()
{
Person::input();
cout<<"请输入学号:"; cin>>stu_no;
{
public:
void fun()
{
cout<<"Base::fun"<<endl;
}
};
class Derived:public Base
{
_______________
void fun()
{
__________________________ //调用基类的函数fun()
cout<<"Derived::fun"<<endl;
a) Person类包含最基本的共同信息
b) Student类继承Person类,并包含自己的私有成员(成员变量和成员函数)
c) Teacher类继承Person类,并包含自己的私有成员(成员变量和成员函数)
程序源代码(注意添加注释)
#include <iostream>
#include <string>
相关主题