C++ 继承
时间:2010-11-17 来源:luozhiyong131
/***********
公有继承
************/
#include <iostream.h>
#include <string.h>
class Student
{
private:
char *name;
int no;
public:
Student(char *nstr="",int sn=1)
{
name=new char[strlen(nstr)+1];
strcpy(name,nstr);
no=sn;
}
~Student(){delete []name;}
char *getname(){return name;}
int getscoreno(){return no;}
};
class GStudent:public Student
{
private:
char *instructor;
public:
GStudent(char *nstr="",int sn=1,char *dstr=""):Student(nstr,sn)
{
instructor=new char[strlen(dstr)+1];
strcpy(instructor,dstr);
}
~GStudent(){delete []instructor;}
char *getinstructor(){return instructor;}
};
void main()
{
GStudent lj("罗志勇",1001,"何");
cout<<"学生罗志勇信息:\n姓名\t学号\t导师"<<endl;
cout<<lj.getname()<<"\t"<<lj.getscoreno()<<"\t"<<lj.getinstructor()<<endl;
}
/***********
私有继承
************/
#include <iostream.h>
#include <string.h>
class Student
{
protected:
char *name;
int no;
public:
Student(char *nstr="",int sn=1)
{
name=new char[strlen(nstr)+1];
strcpy(name,nstr);
no=sn;
}
~Student(){delete []name;}
char *getname(){return name;}
int getscoreno(){return no;}
};
class GStudent:private Student
{
private:
char *instructor;
public:
GStudent(char *nstr="",int sn=1,char *dstr=""):Student(nstr,sn)
{
instructor=new char[strlen(dstr)+1];
strcpy(instructor,dstr);
}
~GStudent(){delete []instructor;}
char *getname(){return name;}
int getscoreno(){return no;}
char *getinstructor(){return instructor;}
};
void main()
{
GStudent lj("罗志勇",1001,"何");
cout<<"学生罗志勇信息:\n姓名\t学号\t导师"<<endl;
cout<<lj.getname()<<"\t"<<lj.getscoreno()<<"\t"<<lj.getinstructor()<<endl;
}
/***********
保护继承
************/
#include <iostream.h>
#include <string.h>
class Student
{
protected:
char *name;
int no;
public:
Student(char *nstr="",int sn=1)
{
name=new char[strlen(nstr)+1];
strcpy(name,nstr);
no=sn;
}
~Student(){delete []name;}
char *getname(){return name;}
int getscoreno(){return no;}
};
class GStudent:protected Student
{
private:
char *instructor;
public:
GStudent(char *nstr="",int sn=1,char *dstr=""):Student(nstr,sn)
{
instructor=new char[strlen(dstr)+1];
strcpy(instructor,dstr);
}
~GStudent(){delete []instructor;}
char *getname(){return name;}
int getscoreno(){return no;}
char *getinstructor(){return instructor;}
};
void main()
{
GStudent lj("罗志勇",1001,"何");
cout<<"学生罗志勇信息:\n姓名\t学号\t导师"<<endl;
cout<<lj.getname()<<"\t"<<lj.getscoreno()<<"\t"<<lj.getinstructor()<<endl;
}
/**********
多继承
***********/
#include <iostream.h>
class MyclassA
{
private:
int a,b;
public:
MyclassA(int=0,int=0);
void displayA();
};
MyclassA::MyclassA(int x,int y)
{
a=x; b=y;
}
void MyclassA::displayA()
{
cout<<"MyclassA a="<<a<<" b="<<b<<endl;
}
class MyclassB
{
private:
int c;
public:
MyclassB(int=0);
void displayB();
};
MyclassB::MyclassB(int cc)
{
c=cc;
}
void MyclassB::displayB()
{
cout<<"MyclassB c="<<c<<endl;
}
class MyclassC : public MyclassA,public MyclassB
{
private:
int d;
public:
MyclassC(int=0,int=0,int=0,int=0);
void displayC();
void display();
};
MyclassC::MyclassC(int aa,int bb,int cc,int dd) : MyclassA(aa,bb),MyclassB(cc)
{
d=dd;
}
void MyclassC::displayC()
{
cout<<"MyclassC d="<<d<<endl;
}
void MyclassC::display()
{
MyclassA::displayA();
MyclassB::displayB();
cout<<"MyclassC d="<<d<<endl;
}
void main()
{
MyclassC c1,c2(3,4,5);
c1.displayA();
c1.displayB();
c1.displayC();
c2.display();
}