您的位置:首页 > 编程语言 > C语言/C++

C++学习基础九——继承

2016-09-17 22:55 447 查看
1.不同于Java中通过extends实现继承,C++是通过:实现的。

2.C++中同样包含public,private,protected三个关键字:

public关键字表示在任意其他类中可调用该成员。

private关键字表示该成员只能在声明该成员的类中使用。

protected关键字用于继承,可在本类中调用声明为protected的成员,也可以在子类中通过子类对象调用,而不能通过父类对象调用。

3.virtual关键字表示该函数可以被子类继承并重写。

如果父类的成员函数声明为virtual,则子类可以重新定义该成员函数,重新定义时去掉virtual关键字。

如果父类的成员函数是普通的函数,则子类继承之后不能重新定义该函数。

4.代码片段如下:

#include <iostream>
#include <string>

using namespace std;

class Item_base
{
public:
Item_base(const string &is,double p):isbn(is),price(p){}
string book()
{
return isbn;
}
virtual double calMoney(int c)
{
return c * price;
}
private:
string isbn;
protected:
double price;
};

class Bulk_item : public Item_base
{
public:
Bulk_item::Bulk_item(const string &is,double p,int c,double dis):
Item_base(is,p),min_py(c),discocunt(dis){}
double calMoney(int c)
{
cout<<endl<<"子类"<<endl;
if(c > min_py)
{
return c * price * discocunt;
}
else
{
return c * price;
}
}
void test()
{
cout<<endl<<"子类测试!!"<<endl;
}
private:
int min_py;
double discocunt;
};

int main()
{
string itemIsbn = "x-123-1234-x";
Item_base item(itemIsbn,10.0);
cout<<item.calMoney(100)<<endl;

Bulk_item item2("123-456-x",10.0,30,0.8);
cout<<item2.calMoney(100)<<endl;
item2.test();

Item_base *item3 = new Bulk_item("123-456-789-x",10.0,10,0.8);
cout<<item3->calMoney(20)<<endl;
//不能调用子类的test方法
return 0;
}


5.C++中的继承不同于Java,C++支持公有继承、私有继承和受保护的继承。

公有继承又称为接口继承,私有继承和受保护继承又称为实现继承。最常用的继承方式是公有继承。

私有继承是指将父类中的public,protected的成员变为private。

受保护继承是指将父类中public的成员变成protected的。

public 继承:

class A
{
public :
A()
{
a = 10;
b = 20;
c = 30;
}
int a;
protected:
int b;
private:
int c;
};

class B1 : public A
{
public :
void test()
{
cout<<"公有继承  "<<a<<" , "<<b<<endl;
//cout<<c<<endl;
}
};


private继承:

class B2 : private A
{
//私有继承将A中public protected的成员变为private
public :
void test()
{
cout<<"私有继承  "<<a<<" , "<<b<<endl;
//cout<<c<<endl;
}
};


protected继承:

class B3 : protected A
{
//受保护继承将A中public的成员变为protected
public:
void test()
{
cout<<"受保护继承  "<<a<<" , "<<b<<endl;
}
};


6.可以通过修改继承访问方式实现去除个别成员(关键字是using)。

例如父类A中public 的成员,通过私有继承时,父类的public成员变为私有的,可以通过using A::a;语句实现公有。

class B2 : private A
{
//私有继承将A中public protected的成员变为private
public :
using A::a2;//将a2变为public 的
void test()
{
cout<<"私有继承  "<<a<<" , "<<b<<endl;
//cout<<c<<endl;
}
};


7.默认继承访问级别:

(1)class默认的继承是私有的,其内的成员默认的也是私有的。

(2)struct默认的继承是公有的,其内的成员默认的也是公有的。

(3)C++中除了默认的继承访问级别不同,class和struct是相同的。

class D : A//默认的继承是private
{
int a;//默认的成员变量是private
};

struct E : A//默认的继承是public
{
int a;//默认的是public
};


8.整体代码如下:

class A
{
public :
A()
{
a = 10;
b = 20;
c = 30;
a2 = 40;
}
int a;
int a2;
protected:
int b;
private:
int c;
};

class D : A//默认的继承是private
{
int a;//默认的成员变量是private
};

struct E : A//默认的继承是public
{
int a;//默认的是public
};

class B1 : public A
{
public :
void test()
{
cout<<"公有继承  "<<a<<" , "<<b<<endl;
//cout<<c<<endl;
}
};

class B2 : private A
{
//私有继承将A中public protected的成员变为private
public :
using A::a2;//将a2变为public 的
void test()
{
cout<<"私有继承  "<<a<<" , "<<b<<endl;
//cout<<c<<endl;
}
};

class B3 : protected A
{
//受保护继承将A中public的成员变为protected
public:
void test()
{
cout<<"受保护继承  "<<a<<" , "<<b<<endl;
}
};

int main()
{
B1 b1;
cout<<b1.a<<endl;
//    cout<<b1.b<<endl;//不可访问
//    cout<<b1.c<<endl;//不可访问
b1.test();

B2 b2;
cout<<b2.a2<<endl;
//    cout<<b2.a<<endl;//不可访问
//    cout<<b2.b<<endl;//不可访问
//    cout<<b1.c<<endl;//不可访问
b2.test();

B3 b3;
//cout<<b3.a<<endl;//不可访问
//cout<<b3.b<<endl;//不可访问
//cout<<b3.c<<endl;//不可访问
b3.test();
return 0;
}


后续更新中....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: