继承和派生(c++)

 继承:在定义一个新的类B时,如果该类与某个已有的类A相似(指的是B拥有A的全部特点),那么就可以把A作为一个基类,而把B作为基类的一个派生类(也称子类)。

派生类是通过对基类进行修改和扩充得到的。在派生类中,可以扩充新的成员变量和成员函数。 派生类一经定义后,可以独立使用,不依赖于基类。

派生类拥有基类的全部成员函数和成员变量,不论是private、protected、public

在派生类的各个成员函数中,不能访问基类中的private成员。

派生类的写法:

class 派生类名:public 基类名 
{

};

派生类对象的内存空间:

派生类对象的体积,等于基类对象的体积,再加上派生类对象自己的成员变量的体积。在派生类对象中,包含着基类对象,而且基类对象的存储位置位于派生类对象新增的成员变量之前。

 

 

这个是我自己简单写的:

#include"iostream"
using namespace std;
class father{     //基类 
	public:
		string name;
	public:
		void setname(const string &name_){name=name_;}
		string getname()
		{
			return name;
		 } 
};
class son :public father{
	private:
		int money;
	public:
		void setin(const string & name_,int m)
		{
			father:: setname(name_);
			money=m;
		}
		void print()
		{
			cout<<getname()<<endl;
			cout<<money<<endl;
		}
}; 
int main()
{
	son s;
    father f;
	f.setname("liu");
    s.setin("jia",8);
	s.print();
	return 0;
}

2. 

#include <iostream>
#include <string>
using namespace std;
class CStudent  {
    private:
        string name;
        string id;  //学号
        char gender; //性别,'F'代表女,'M'代表男
        int age;
    public:
        void PrintInfo();
        void SetInfo( const string & name_,const string & id_,
	 int age_,       char gender_ );
        string GetName() {  return name;  }
};
class CUndergraduateStudent:public CStudent 
{//本科生类,继承了CStudent类
    private:
        string department; //学生所属的系的名称
    public:
        void QualifiedForBaoyan() {   //给予保研资格
            cout << "qualified for baoyan "<< endl;
        }
        void PrintInfo() {
            CStudent::PrintInfo(); //调用基类的PrintInfo
            cout << "Department:" << department <<endl;
        }
        void SetInfo( const string & name_,const string & id_,
            int age_,char gender_ ,const string &  department_) {
            CStudent::SetInfo(name_,id_,age_,gender_); //调用基类的SetInfo
            department = department_;
        }
};
void CStudent::PrintInfo()
{
	cout << "Name:" << name << endl;
	cout << "ID:" << id << endl;
	cout << "Age:" << age << endl;
	cout << "Gender:" << gender << endl;
}
void CStudent::SetInfo( const string & name_,const string & id_,   
                                      int age_,char gender_ )
{
	name = name_;
	id = id_;
	age = age_;
	gender = gender_;
}
int main()
{

    CUndergraduateStudent s2;
     s2.SetInfo("Harry Potter" ,  "118829212",19,'M',"Computer Science");
     cout <<  s2.GetName() << " " ;
     s2.QualifiedForBaoyan ();
     s2.PrintInfo ();
     return 0;
}

输出:

Harry Potter qualified for baoyan

Name:Harry Potter

ID:118829212

Age:19

Gender:M

Department:Computer Science

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘家奕_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值