当前位置:编程学习 > C/C++ >>

速度高低求 c++ 编程创建person类

创建一个person类,属性包括 姓名、性别、出生日期,然后从person类派出一个教师类,新增加的属性有:专业、职称和主讲课程(一门)并要求定义相应的设置属性的成员函数和输出信息的成员函数
答案:class.h

---------------------------------------------------------

#include <string>

#ifndef CLASS_H_
#define CLASS_H_

class Person
{
private:
std::string name;
std::string 易做图;
std::string birth;
public:
Person() { }
void set();
void show();
~Person() { }
};

class Teacher:public Person
{
private:
std::string pro;
std::string title;
std::string course;
public:
Teacher():Person() { }
void set();
void show();
~Teacher() { }
};

#endif

class.cpp

--------------------------------------------

#include <iostream>
#include "class.h"
using namespace std;

void Person::set()
{
cout<<"Please input your name:";
cin>>name;
cout<<"Please input your 易做图:";
cin>>易做图;
cout<<"Please input your birthday:";
cin>>birth;
}

void Person::show()
{
cout<<"Name:"<<name<<endl;
cout<<"Sex:"<<易做图<<endl;
cout<<"Birthday:"<<birth<<endl;
}

void Teacher::set()
{
Person::set();
cout<<"Please input your profession:";
cin>>pro;
cout<<"Please input your title:";
cin>>title;
cout<<"Please input your course:";
cin>>course;
}

void Teacher::show()
{
Person::show();
cout<<"Profeesion:"<<pro<<endl;
cout<<"Title:"<<title<<endl;
cout<<"Course:"<<course<<endl;
}

main.cpp

-------------------------------------------

#include <iostream>
#include "class.h"

int main()
{
using namespace std;
Person one;
one.set();
cout<<endl;
one.show();
cout<<endl;
Teacher two;
two.set();
cout<<endl;
two.show();
}

class person{

private:

char name[81];

public:

virtual void print(){ cout<<"Name: "<<name<<endl; }

};

class teacher:public person{

private:

int salary;

public:

virtual void print()

{

person::print();

cout<<"Salary: "<<salary<<endl;

}

};

void main()

{

person p;

person.print();

teacher t;

t.print();

}

上一个:c++程序设计的特点在哪呀???
下一个:想学C++ 有什么书推荐 基础点的

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,