[C + +] base class object as a function parameter (Assignment compatibility rule)

Source: Internet
Author: User

Programming to handle the basic situation of teachers. Requirements:

1, define a "person" class, used to store and deal with the name, gender, age, member functions of the self-defined;

2, the definition of "teacher" category, the public inherits "Person" class to store the teacher's school, the major, education, degree, title, seniority, etc., member functions of the self-determined.

3, processing procedures, mainly include:

⑴ display Name, gender, age function: Displays the name , gender, age of the person object, and displays the name, gender, and age of the teacher object (referenced by person as a parameter) ;

⑵ shows the function of Teacher's college, major, degree, degree, title and seniority;

⑶main () function: Define persor object and teacher object Separately, enter different object related values, call member function to set the value of object, call display function to display corresponding value.


#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::p Erson (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;//College Char *speciality;//professional char *school;//degree char *degree;//degree char *title;//title int teacherage;//seniority    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):p Erson (na,se,ag) {//pers    On::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[strle    N (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", +);    Teacher T ("Miss Zhang", "female", 44, "Department of Science", "Network", "undergraduate", "Doctor", "Professor");    t.print_t (p);    T.set_t ("Miss Zhang", "female", 44, "department", "Network", "undergraduate", "Doctor", "Professor",);    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", +//    p.print ();//    P.set_person ("W", "female", and "/");//    P.print ();//    return 0;//}


[C + +] base class object as a function parameter (Assignment compatibility rule)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.