派生与继承

包含知识点:

1.函数在基类与派生类中同名,派生类对象访问同名函数将调用派生类的函数,基类对象访问同名函数将调用基类的函数

2.派生类构造函数负责基类的构造(详细知识点位于书本,151-152的说明(1)(2)(3),包括派生类什么情况下负责基类的构造)

3.this指针的作用(假如,有同名,this将指向类的成员,而不是函数里面的成员)

4.函数成员的类外定义与类内定义(书本53)

#include <iostream>
using namespace std;

class classroom
{
private:
double price;
public:
classroom(double num){this->price=num;}
void Show_price()
{
cout<<"classroom cost "<<this->price<<"."<<endl;
}
};

class stool : public classroom
{
private:
double price;
public:
stool(double num1, double num) : classroom(num1)
{ this->price=num;}
void Show_price();
};

void stool::Show_price()
{
cout<<"stool cost "<<this->price<<"."<<endl;
}

int main()
{
classroom two(10000);
two.Show_price();
stool one(1000,10);
one.Show_price();
}

 不懂请问

posted @ 2020-08-23 14:36  Conan-jine  阅读(131)  评论(0编辑  收藏  举报