!!!C++ sample code

1. 类继承

继承时,子类中含有父类所有的public & protected & private member. 即使在子类中定义同名成员,也可以通过scope区分:

#include<iostream>
using namespace std;
class A {
public:
	int x;
protected:
	int z;
private:
	int y;
};
class B: public A {
public:
	int	x;
protected:
	int z;
private:
	int y;
};
int main()
{
	cout << sizeof(A)<<endl;     //12
	cout << sizeof(B)<<endl;     //24
}


#include<iostream>
#include<stdio.h>
using namespace std;
class base {
public:
	int fun () { return x + y; }
	base (int i = 0, int j = 0) : x(i), y(j) {}
	int x;
private:
	int y;
};

class derived1 : public base {
	int fun () {return 0; }
};

class derived2 : public base {
public:
	derived2 (int i) : x(i) {}
	int x;
};
void main()
{
	cout << sizeof (base) << endl;
	cout << sizeof(derived1) << endl;
	cout << sizeof(derived2) << endl;
	derived2 d2(1);
	derived2* d2p = &d2;
	cout << d2.x << endl;
	cout << d2.base::x << endl;
	cout << d2p->derived2::x << endl;
	cout << d2p->base::x << endl;
}

output:


2. Using default Argument in a Derived Constructor P 582

we should always give default argument in the derived constructor, and the default argument should be consistent with the base class' constructor:

#include<iostream>
#include<stdio.h>
using namespace std;
struct base {
	base (int i = 0, int j = 0) : x(i), y(j) {}
	int x;
	int y;
};
struct derived1 : public base {
	derived1 (int i = 1, int j = 1, int k = 1) : base (i,j), z(k) {}
	int z;
};
struct derived2 : public base {
	derived2 (int i, int j, int k = 1) : base (i,j), z(k) {}
	int z;
};
void main()
{
	derived1 test;
	cout << test.x << endl;
	cout << test.y << endl;
	cout << test.z << endl;
	derived2 tt;
	cout << tt.x << endl;
	cout << tt.y << endl;
	cout << tt.z << endl;
}

Result:

test can be created successfully: x = 1, y = 1, z = 1.

tt will give error: Error    1    error C2512: 'derived2' : no appropriate default constructor available

3. 类赋值

derived class will inherits all members from the base class. And we can use the derived class to assign/initialize the base class,even with the private member(The derived class access the private member through Base Class' function):

#include<iostream>
#include<stdio.h>
using namespace std;
struct base {
	base (int i = 0) : x(i) {}
	int Fun() { return x; }
private:
	int x;
};
struct derived : public base {             
	derived (int i = 1) : base (i) {}
};
void main()
{
	cout << sizeof(base) << endl;       // 4
	cout << sizeof(derived) << endl;    // 4
	derived de(5); 
//	cout << de.x << endl;               //not accessible
	cout << de.Fun() << endl;           // 5
	base ba(de);
	cout << ba.Fun() << endl;           // 5
}

4. Public, Protected, Private inheritance

Only public inherited class can do dynamic binding & assign/initialize base class:

#include<iostream>
#include<stdio.h>
using namespace std;
struct base {
	base (int i = 0) : x(i) {}
	int Fun() { return x; }
	int x;
};
struct derived : public base {
	derived (int i = 1) : base (i) {}
};

struct derived1 : protected base {
	derived1 (int i = 1) : base (i) {}
};

struct derived2 : private base {
	derived2 (int i = 1) : base (i) {}
};

void main()
{
	derived a(5);
	derived1 b(5);
	derived2 c(5);
	base d;
	d=a;           // ok
	d=b;           // conversion to inaccessible base class "base" is not allowed
	d=c;           // conversion to inaccessible base class "base" is not allowed
	base *e;
	e=&a;          // ok
	e=&b;          // conversion to inaccessible base class "base" is not allowed
	e=&c;          // conversion to inaccessible base class "base" is not allowed
}

5. Base Derived class Conversion & Containers and Inheritance

#include<iostream>
#include<string>
#include<set>
using namespace std;
class base {
public:
	base (int a = 0, int b = 0) : x(a), y(b) {}
	int x;
	int value() const { return y; }    //has to be const, or iterator cannot get value!
	friend bool operator< (const base & lhs, const base & rhs) {
		return lhs.x<rhs.x;
	}
private:
	int y;
};
class derived1 : public base {
public:
	derived1(int a = 0, int b = 0, int c = 0) : base (a,b), z(c) {}
	int z;
};
class derived2 : private base {
public:
	derived2(int a = 0, int b = 0, int c = 0) : base (a,b), z(c) {}
	int z;
};
int main()
{
	base a;
	derived1 b(1,2,3);
	derived2 c(4,5,6);
	a = b;
	cout << a.value() << endl;       // ok, output is 2
//	a = c;
//	cout << a.value() << endl;       // Eerror : conversion from 'derived2 *' to 'const base &' exists, but is inaccessible
	multiset<base> basket;
	basket.insert(b);
	multiset<base>::iterator it = basket.begin();
	cout << it->value() << endl;     // ok, output is 2
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值