例7-1:
#include<iostream>
#include<cmath>
using namespace std;
class point {
public:
void initpoint(float x = 0, float y = 0) { this->x = x; this->y = y; }
void move(float offx, float offy) { x += offx; y += offy; }
float getx()const { return x; }
float gety()const { return y; }
private:
float x, y;
};
class rectangle:public point {
public:
void initrectangle(float x, float y, float w, float h) {
initpoint(x, y);
this->w = w;
this->h = h;
}
float geth()const { return h; }
float getw()const { return w; }
private:
float w, h;
};
int main()
{
rectangle rect;
rect.initrectangle(2, 3, 20, 10);
rect.move(3, 2);
cout << "the data of rect(x,y,w,h):" << endl;
cout << rect.getx() << ","
<< rect.gety() << ","
<< rect.getw() << ","
<< rect.geth() << endl;
return 0;
}
例7-2:
#include<iostream>
#include<cmath>
using namespace std;
class point {
public:
void initpoint(float x = 0, float y = 0) { this->x = x; this->y = y; }
void move(float offx, float offy) { x += offx; y += offy; }
float getx()const { return x; }
float gety()const { return y; }
private:
float x, y;
};
class rectangle :private point { //私有继承不可访问父类函数;
public:
void initrectangle(float x, float y, float w, float h) {
initpoint(x, y);
this->w = w;
this->h = h;
}
float geth()const { return h; }
float getw()const { return w; }
private:
float w, h;
};
int main()
{
rectangle rect;
rect.initrectangle(2, 3, 20, 10);
rect.move(3, 2);
cout << "the data of rect(x,y,w,h):" << endl;
cout << rect.getx() << ","
<< rect.gety() << ","
<< rect.getw() << ","
<< rect.geth() << endl;
return 0;
}
例7-3:
#include<iostream>
using namespace std;
class base1{
public:
void display()const { cout << "base1::display()" << endl; }
};
class base2:public base1 {
public:
void display()const { cout << "base2::display()" << endl; }
};
class derived :public base2 {
public:
void display()const { cout << "derived::display()" << endl; }
};
void fun(base1*ptr) {
ptr->display();
}
int main()
{
base1 base1;
base2 base2;
derived derive;
fun(&base1);
fun(&base2);
fun(&derive);
return 0;
}
例7-4:
#include<iostream>
using namespace std;
class base1 {
public:
base1(int i) { cout << "constructing base1" << i << endl; }
};
class base2 {
public:
base2(int j) { cout << "construcrting base2" << j << endl; }
};
class base3 {
public:
base3() { cout << "construing base3 *" << endl; }
};
class derived :public base2, public base1, public base3 {
public:
derived(int a, int b, int c, int d) :base1(a), member2(d), member1(c), base2(b){
}
private:
base1 member1;
base2 member2;
base3 member3;
};
int main() {
derived obj(1, 2, 3, 4);
return 0;
}
标签:const,第一,float,class,base1,return,打卡,public From: https://www.cnblogs.com/hlhl/p/17311301.html