//7-5
#include<iostream>
using namespace std;
#define PI 3.14
class Shape
{
public:
virtual float getarea() {
return -1;
}
};
class Rectangle :public Shape
{
public:
Rectangle(){}
Rectangle(int m_x,int m_y):x(m_x),y(m_y){}
float getarea()
{
return x * y;
}
private:
float x, y;
};
class Cricle :public Shape
{
public:
Cricle(){}
Cricle(int m_r):r(m_r){}
float getarea()
{
return PI*r*r;
}
private:
int r;
};
class Squre :public Rectangle
{
public:
Squre(float len):Rectangle(len,len){}
};
//Squre::Squre(float len) :
// Rectangle(len, len)
//{
//
//}
int main()
{
Shape *s;
s = new Cricle(5);
cout << s->getarea() << endl;
delete s;
s = new Rectangle(4, 5);
cout << s->getarea() << endl;
delete s;
s = new Squre(10);
cout << s->getarea() << endl;
delete s;
}
//7-6
#include<iostream>
using namespace std;
#define PI 3.14
class Malmal
{
public:
Malmal()
{
cout << "父类构造函数的调用" << endl;
}
~Malmal()
{
cout << "父类析构函数的调用" << endl;
}
};
class Dog :public Malmal
{
public:
Dog()
{
cout << "子类构造函数的调用" << endl;
}
~Dog()
{
cout << "子类析构函数的调用" << endl;
}
};
int main()
{
Dog d;
}
//7-7
#include<iostream>
using namespace std;
#define PI 3.14
class Malmal
{
public:
Malmal()
{
cout << "父类无参构造函数的调用" << endl;
}
Malmal(string m_name, int m_age) :name(m_name), age(m_age) { cout << "父类有参函数的调用" << endl; }
void display()
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age <<endl;
}
~Malmal()
{
cout << "父类析构函数的调用" << endl;
}
private:
int age;
string name;
};
class Dog :public Malmal
{
public:
Dog()
{
cout << "子类无参构造函数的调用" << endl;
}
Dog(string name, int age) :Malmal(name, age) { cout << "子类有参构造函数的调用" << endl; }
~Dog()
{
cout << "子类析构函数的调用" << endl;
}
};
int main()
{
Dog d("小黄", 10);
d.display();
}
//7-8
#include<iostream>
using namespace std;
#define PI 3.14
class Document
{
public:
Document(){}
Document(string m_name) :name(m_name){}
void display()
{
cout << "书的名字:" << name<<endl;
}
private:
string name;
};
class Book :public Document
{
public:
Book(){}
Book(string name,int count):Document(name), pagecount(count){}
void display()
{
cout << "书的页数:" << pagecount << endl;
}
int pagecount;
};
int main()
{
Book b("平凡的世界",689);
b.Document::display();
b.display();
}//7-9
#include<iostream>
using namespace std;
#define PI 3.14
class Base
{
public:
Base(){}
int fn1()
{
cout << "fn1函数调用" << endl;
return 1;
}
int fn2()
{
cout << "fn2函数的调用" << endl;
return 2;
}
};
class Derived:private Base
{
public:
Derived(){}
int fn1()
{
return Base::fn1();
}
int fn2()
{
return Base::fn2();
}
};
int main()
{
Derived m;
m.fn1();
m.fn2();
}//7-10
#include<iostream>
using namespace std;
#define PI 3.14
class Object
{
public:
Object(){ cout << "父类无参函数的调用" << endl; }
Object(int m_weight) :weight(m_weight) { cout << "父类有参函数的调用" << endl; }
~Object() { cout << "父类析构函数的调用" << endl; }
private:
int weight;
};
class Box:private Object
{
public:
Box(){ cout << "子类无参函数的调用" << endl; }
Box(int m_weight,int m_height, int m_width) :Object(m_weight),height(m_height), width(m_width) { cout << "子类有参函数的调用" << endl; }
~Box() { cout << "子类析构函数的调用" << endl; }
private:
int height;
int width;
};
int main()
{
Box m;
Box n(10, 20, 30);
}//7-11
#include<iostream>
using namespace std;
#define PI 3.14
class Baseclass
{
public:
Baseclass() {}
void fn1()
{
cout << "父类fn1函数调用" << endl;
}
void fn2()
{
cout << "父类fn2函数的调用" << endl;
}
};
class Derivedclass :public Baseclass
{
public:
Derivedclass() {}
void fn1()
{
cout << "子类fn1函数调用" << endl;
}
void fn2()
{
cout << "子类fn2函数调用" << endl;
}
};
int main()
{
Derivedclass m;
Derivedclass* p = &m;
Baseclass* s = &m;
m.fn2();
m.Baseclass::fn2();
p->fn1();
p->fn2();
s->fn1();
s->fn2();
}
//7-13