[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 ...
随机推荐
- Dictionary as a set of counters
Suppose you are given a string and you want to count how many times each letters appears. There are ...
- ui5 call view or method from another view
// call view or method from another view //# view call // var view2=sap.ui.jsview("ui5d.popup01 ...
- How Javascript works (Javascript工作原理) (十四) 解析,语法抽象树及最小化解析时间的 5 条小技巧
个人总结:读完这篇文章需要15分钟,文章介绍了抽象语法树与js引擎解析这些语法树的过程,提到了懒解析——即转换为AST的过程中不直接进入函数体解析,当这个函数体需要执行的时候才进行相应转换.(因为有的 ...
- Win 7系统倒计时!
3月25日消息,近日微软已经开始通知当前正在使用Windows 7的用户,该操作系统“接近尾声”.微软表示计划在2020年1月14日终止对Windows 7的所有支持.但结束Windows 7似乎并不 ...
- 20180929 北京大学 人工智能实践:Tensorflow笔记05
(完)
- Unity C# 关于设计模式的思考
一.当你的项目发现有如下问题之一时,就需要考虑重构代码,可能会有某种模式适合. 1.代码无法进行单元测试. 2.需求的变动总是导致代码的变动. 3.有重复代码的存在. 4.继承层次过多. 5.隐藏的依 ...
- Java基础学习总结(23)——GUI编程
一.AWT介绍 所有的可以显示出来的图形元素都称为Component,Component代表了所有的可见的图形元素,Component里面有一种比较特殊的图形元素叫Container,Containe ...
- Httphelper头信息(ContentType)默认为text/html无懈可击
Httphelper头信息(ContentType)默认为text/html无懈可击转 http://www.sufeinet.com/thread-8623-1-1.html 我发现最近有几个网友提 ...
- 【iOS开发-34】自己主动释放池@autoreleasepool的使用注意事项以及ARC机制——面试必考内容
自己主动释放池@autorelease面试频率可能会吧release还要高. (1)在自己主动释放池@autoreleasepool{}中alloc一个对象后(如p1).仍然须要用[p1 autore ...
- EBS 第一个项目 学习总结 ---- 发运模块
EBS 组织架构: (一)业务组(BG) (二)法律实体(LE) (三)业务实体(OU) (四)库存组织(INV) (五)公司成本中心(Cost Center) (六)HR组织 (七)多组织接入控制 ...