日志19
类的继承与派生;
#include<bits/stdc++.h>
using namespace std;
class Shape
{
protected:
double s;
public:
void show() { cout << "s=" << s << endl; }
};
class Rectangle :public Shape
{
protected:
double a;
double b;
public:
Rectangle(double A = 1, double B = 1) :a(A), b(B) { s = a * b; }
void set(double A, double B) { a = A;b = B; }
double geta() { return a; }
double getb() { return b; }
double gets(){return s;}
};
class yuan :public Shape
{
protected:
double r;
public:
yuan(double a){s=3.14*a*a;}
void setyuan(double R) { r = R; }
double getr() { return r; }
double gets(){return s;}
};
class square :public Rectangle
{
public:
square(double a) :Rectangle(a, a){}
double gets(){return s;}
};
继承就是为了提高代码的复用性,避免写大段的,重复的代码;
在父类的基础上进行修改和添加得到子类,就不用再重复父类的部分。
