任务1
#include<iostream>
using namespace std;
class Mammal
{
public:
Mammal() { cout << "Mammal" << endl; }
~Mammal() { cout << "~Mammal" << endl; }
};
class Dog:public Mammal
{
public:
Dog() { cout << "Dog" << endl; }
~Dog() { cout << "~Dog" << endl; }
};
class Document
{
private:
char name[100];
};
class Book :public Document
{
private:
int pageCount;
};
class Base
{
public:
void fn1() { cout << "Base fn1" << endl; }
void fn2() { cout << "Base fn2" << endl; }
};
class Derived :public Base
{
public:
void fn1() { Base::fn1(); }
};
int main()
{
Derived der;
der.Base::fn1();
return 0;
}
任务2
#include<iostream>
using namespace std;
class Shape
{
public:
Shape() { line_col = fill_col = line_rid = pen_style = 0; }
~Shape() {}
void Set_line_col() {}
void Set_fill_col() {}
void Set_line_rid() {}
void Set_pen_style() {}
private:
int line_col;
int fill_col;
int line_rid;
int pen_style;
};
class Point
{
public:
Point() { x = 0, y = 0; }
void initPoint(float x = 0, float y = 0)
{
this->x = x; this->y = y;
}
void movePoint(float offX, float offY)
{
x += offX;
y += offY;
}
float getX() { return x; }
float getY() { return y; }
private:
float x, y;
};
class Rectangle :public Point, public Shape
{
public:
Rectangle(int w, int h) :width(w), height(h) {}
~Rectangle() { cout << "~Rectangle" << endl; }
int getArea()
{
return width * height;
}
void Set(int w, int h)
{
width = w;
height = h;
}
private:
int height, width;
};
class Square :public Rectangle
{
public:
Square(int l) :Rectangle(l, l) {}
void Set(int l) { Rectangle::Set(l, l); }
};
class Circle :public Point, public Shape
{
public:
Circle(int r) :radius(r) {}
~Circle() { cout << "~Circle" << endl; }
double GetArea() { return 3.1415926 * radius * radius; }
private:
int radius;
};
int main()
{
Rectangle rect(5, 10);
cout << rect.getArea() << endl;
cout << rect.getX() << ' ' << rect.getY() << endl;
rect.movePoint(20, 50);
cout << rect.getX() << ' ' << rect.getY() << endl;
Circle cir(3);
cout << cir.GetArea() << endl;
Square squ(5);
cout << squ.getArea() << endl;
return 0;
}