[c++]基类对象作为函数參数(赋值兼容规则)
编程处理教师的基本情况。
要求:
1、定义一个“person”类。用来存储及处理人的姓名、性别、年龄,成员函数自定;
2、定义“teacher”类,公有继承“person”类用来存储教师所在学院、所学专业、学历、学位、职称、教龄等,成员函数自定。
3、处理程序,主要包含:
⑴显示姓名、性别、年龄函数:既能显示person对象的姓名、性别、年龄,又能显示teacher对象的姓名、性别、年龄(用person引用对象为形參);
⑵显示教师所在学院、所学专业、学历、学位、职称、教龄的函数;
⑶ main()函数:分别定义persor对象及teacher对象,并输入不同对象相关值。调用成员函数设置对象的值,调用显示函数显示对应值。
#ifndef __person__person__
#define __person__person__
#include<iostream>
using namespace std;
#include<string.h>
class person
{
protected:
char *name;
char *sex;
int age;
public:
person(char *na,char *se,int ag);
void set_person(char *na,char *se,int ag);
char* get_name()
{return name;}
char* get_sex()
{return sex;}
int get_age()
{return age;}
void print();
~person()
{delete []name;delete []sex;} }; person::person(char *na,char *se,int ag)
{ name=new char[strlen(na)+1];
strcpy(name,na);
sex=new char[strlen(se)+1];
strcpy(sex,se);
age = 0;
} void person:: set_person(char *na,char *se,int ag)
{
name=new char[strlen(na)+1];
strcpy(name,na);
sex=new char[strlen(se)+1];
strcpy(sex,se);
age=ag;
} void person:: print()
{
for(int i = 0;i<=strlen(name);i++)
{
cout<<name[i];
}
cout<<endl;
for(int i = 0;i<=strlen(sex);i++)
{
cout<<sex[i];
}
cout<<endl;
cout<<"age:"<<age<<endl;
cout<<endl; } #endif /* defined(__person__person__) */
#ifndef person_teacher_h
#define person_teacher_h #include"person.h"
class teacher:public person
{
protected:
char *college;//学院
char *speciality;//专业
char *school;//学历
char *degree;//学位
char *title;//职称
int teacherage;//教龄
public:
teacher(char *na,char* se,int ag,char *co,char *sp,char* sc,char* de,char* ti,int te);
void set_t(char *na,char* se,int ag,char *co,char *sp,char* sc,char* de,char* ti,int te);
char* get_college()
{return college;}
char* get_speciality()
{return speciality;}
char* get_school()
{return school;}
char* get_degree()
{return degree;}
char* get_title()
{return title;}
int get_teacher_age()
{return teacherage;}
~teacher();
void print_t(person&p);
}; teacher::teacher(char *na,char* se,int ag,char *co,char *sp,char* sc,char* de,char* ti,int te):person(na,se,ag)
{
//person::set_person(na, se, ag);
college=new char[strlen(co)+1];
strcpy(college,co);
speciality=new char[strlen(sp)+1];
strcpy(speciality,sp);
school=new char[strlen(sc)+1];
strcpy(school,sc);
degree=new char[strlen(de)+1];
strcpy(degree,de);
title=new char[strlen(ti)+1];
strcpy(title,ti);
teacherage =0;
} void teacher:: set_t(char *na,char* se,int ag,char *co,char *sp,char* sc,char* de,char* ti,int te)
{
name=new char[strlen(na)+1];
strcpy(name,na);
sex=new char[strlen(se)+1];
strcpy(sex,se);
age=ag; college=new char[strlen(co)+1];
strcpy(college,co);
speciality=new char[strlen(sp)+1];
strcpy(speciality,sp);
school=new char[strlen(sc)+1];
strcpy(school,sc);
degree=new char[strlen(de)+1];
strcpy(degree,de);
title=new char[strlen(ti)+1];
strcpy(title,ti);
teacherage =te; }
teacher::~teacher()
{ delete []college;
delete []speciality;
delete []school;
delete []degree;
delete [] title;
} void teacher:: print_t(person &p)
{
p.print();
cout<<"-------------------"<<endl;
for(int i = 0;i<=strlen(college);i++)
{
cout<<college[i];
}
cout<<endl;
for(int i = 0;i<=strlen(speciality);i++)
{
cout<<speciality[i];
}
cout<<endl;
for(int i = 0;i<=strlen(school);i++)
{
cout<<school[i];
}
cout<<endl;
for(int i = 0;i<=strlen(degree);i++)
{
cout<<degree[i];
}
cout<<endl;
for(int i = 0;i<=strlen(title);i++)
{
cout<<title[i];
}
cout<<endl; cout<<teacherage<<endl;
cout<<endl;
}
void fun(person &p)
{
p.print();
cout<<endl;
} #endif
#include "teacher.h"
//#include"person.h"
int main()
{
person p("s","nv",22);
teacher T("张老师","女",44,"计科","网络","本科","博士","教授",20);
T.print_t(p);
T.set_t("张老师","女",44,"计科","网络","本科","博士","教授",20);
T.print_t(p);
cout<<T.get_name()<<endl;
cout<<T.get_sex()<<endl;
cout<<T.get_age()<<endl;
cout<<T.get_college()<<endl;
cout<<T.get_degree()<<endl;
cout<<T.get_school()<<endl;
cout<<T.get_speciality()<<endl;
cout<<T.get_title()<<endl;
cout<<T.get_teacher_age()<<endl;
cout<<"--------------------"<<endl;
fun(p);
return 0;
} //int main()
//{
// person p("s","nv",22);
// p.print();
// p.set_person("w","female",44);
// p.print();
// return 0;
//}
[c++]基类对象作为函数參数(赋值兼容规则)的更多相关文章
- Effective JavaScript Item 55 接受配置对象作为函数參数
接受配置对象作为函数參数 尽管保持函数接受的參数的顺序非常重要,可是当函数可以接受的參数达到一定数量时.也会让用户非常头疼: var alert = new Alert(100, 75, 300, 2 ...
- C++容器类对象函数參数问题
总之中的一个句话:容器类对象作为函数參数,与整数类型作为函数參数的传递特性同样. 验证程序 #include "stdafx.h" #include <iostream> ...
- 各种python 函数參数定义和解析
python 中的函数參数是赋值式的传递的,函数的使用中要注意两个方面:1.函数參数的定义过程,2.函数參数在调用过程中是怎样解析的. 首先说一下在python 中的函数调用过程是分四种方式的.这里且 ...
- C++ - 虚基类、虚函数与纯虚函数
虚基类 在说明其作用前先看一段代码 class A{public: int iValue;}; class B:public A{public: void bPrintf(){ ...
- C++中基类对象的引用
代码: #include <iostream> #include <cstdio> using namespace std; class A{ public: void pri ...
- C++派生类中如何初始化基类对象(五段代码)
今天收到盛大的面试,问我一个问题,关于派生类中如何初始化基类对象,我在想派生类对于构造函数不都是先构造基类对象,然后在构造子类对象,但是如果我们在成员初始化列表先初始化派生类的私有成员,在函数内去调用 ...
- JavaScript的最大函数參数长度和最大栈深度检測
一般代码也许不会涉及最大參数长度和最大栈深度,但某些特殊场合,检測这两个參数还是有必要的.比如:用递归计算斐波那契数列的第n个值,不了解最大栈深度,难免显得肤浅.又比如:将一串charCode转成St ...
- 关于MFC中重载函数是否调用基类相对应函数的问题
在重载CDialog的OnInitDialog()函数的时候,在首行会添加一句:CDialongEx::OnInitDialog();语句,这是为什么呢?什么时候添加,什么时候不添加? 实际上,我们在 ...
- 业务基类对象BaseBLL
using System; using System.Collections; using System.Data; using System.Text; using System.Collectio ...
随机推荐
- 4. idea常用快捷键设置(改为eclipse相似)
转自:https://blog.csdn.net/loveer0/article/details/82697877 idea常用快捷键设置(改为eclipse相似) 目录 idea常用快捷键设置改为e ...
- Struts2标签库整理【完整】
转自:https://blog.csdn.net/chen_zw/article/details/8161230 Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,str ...
- POJ 3173 模拟
按照题意模拟就好-- //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; in ...
- POJ 3187 全排列+杨辉三角(组合数)
思路: next_permutation()加个递推组合数随便搞搞就A了- //By SiriusRen #include <cstdio> #include <algorithm& ...
- ASP.NET Identity 角色管理(Roles)
当我们使用ASP.NET 4.5创建模板项目时,会发现模板只提供了ApplicationUserManager用于用户的登录注册.修改.设置等,而没有提供与用户角色相关的代码,对此就需要我们自己手动的 ...
- 验证备份集-使用DBVERIFY工具
DBVERIFY确认备份集是否存在坏块 验证TEST03.DBF 文件的是否存在坏块 C:\Documents and Settings\Administrator>dbv file=D:\or ...
- Spring AOP 实现数据库读写分离
背景 我们一般应用对数据库而言都是"读多写少",也就说对数据库读取数据的压力比较大,有一个思路就是说采用数据库集群的方案, 其中一个是主库,负责写入数据,我们称之为:写库: 其它都 ...
- caffe(12) 训练自己的数据
学习caffe的目的,不是简单的做几个练习,最终还是要用到自己的实际项目或科研中.因此,本文介绍一下,从自己的原始图片到lmdb数据,再到训练和测试模型的整个流程. 一.准备数据 有条件的同学,可以去 ...
- 用Python爬取影视网站,直接解析播放地址。
记录时刻! 写这个爬虫主要是想让自己的爬虫实用,把脚本放到了服务器,成为可随时调用的接口. 思路算是没思路吧!把影视名带上去请求影视网站,然后解析出我们需要的播放地址. 我也把自己的接口分享出来.接口 ...
- request.getxxxxxx()的使用方法
request.getSchema() 可以返回当前页面使用的协议,http 或是 https; request.getServerName() 可以返回当前页面所在的服务器的名字; request. ...