类之间的关系
继承:在已有类的基础上创建新类的过程
- 一个B类继承A类,或称从类A派生类B
- 类A称为基类(父类),类B称为派生类(子类)
基类和派生类
派生类的生成过程:
1.吸收基类成员
2.改造基类成员
3.添加派生类新成员
== 例如:==
#include <iostream>
using namespace std;
class A{
public:
int a;
int b;
private:
int c;
protected:
int d;
};
class B: public A
{
int c;
};
int main( ){
cout << ” size of A is” << sizeof(A);
cout << ” size of B is” << sizeof(B);
}
共有继承
#include<iostream>
#include <string>
using namespace std;
class Person
{
string name;
int age;
string sex;
public:
void set_p() {
cout<<"name\tage\tsex"<<endl;
cin>>name>>age>>sex;
}
void show_p() {
cout<<name<<" "<<age<<" "<<sex<<endl;
}
};
class student :public Person
{
string no;
string zhuanye;
string t_class;
float score;
public:
void set_t(){
set_p(); //调用继承于基类的成员函数访问继承于基类的私有数据成员
cout<<"zhuanye\tt_class\tscore"<<endl;
cin>>zhuanye>>t_class>>score;
}
void show_t() {
show_p();
cout<<zhuanye<<" "<<t_class<<" "<<score<<endl;
}
};
重名成员
1.重名数据成员
例如:
class base
{ public :
int a , b ;
} ;
class derived : public base
{ public :
int b , c ;
} ;
void f ()
{ derived d ;
d . a = 1 ;
d . base :: b = 2 ;
d . b = 3 ;
d . c = 4 ;
};
派生类中访问静态成员
例如:
#include<iostream>
using namespace std ;
class B
{ public:
static void Add() { i++ ; }
static int i;
void out() { cout<<"static i="<<i<<endl; }
};
int B::i=0;
class D : private B
{ public:
void f()
{ i=5;
Add();
B::i++;
B::Add();
}
};
int main()
{ B x; D y;
x.Add();
x.out();
y.f();
cout<<"static i="<<B::i<<endl;
cout<<"static i="<<x.i<<endl;
//cout<<"static i="<<y.i<<endl;
}
基类的初始化
#include<iostream>
using namespace std ;
class parent_class
{ int data1 , data2 ;
public :
parent_class ( int p1 , int p2 ) { data1 = p1; data2 = p2; }
int inc1 () { return ++ data1; }
int inc2 () { return ++ data2 ; }
void display () {cout << "data1=" << data1 << " , data2=" << data2 << endl ; }
};
class derived_class : private parent_class
{ int data3 ;
parent_class data4 ;
public:
derived_class ( int p1 , int p2 , int p3 , int p4 , int p5 ): parent_class ( p1 , p2 ) , data4 ( p3 , p4 )
{ data3 = p5 ; }
int inc1 ( ) { return parent_class :: inc1 ( ) ; }
int inc3 ( ) { return ++ data3 ; }
void display ( )
{ parent_class :: display ( ) ; data4.display ( ) ;
cout << "data3=" << data3 << endl ;
}
} ;
int main ( )
{
derived_class d1 ( 17 , 18 , 1 , 2 , -5 ) ; d1 . inc1 ( ) ;
d1 . display ( ) ;
}
派生类构造函数的定义
在c++中,派生类构造函数的一般格式为:
派生类::派生类名(参数总表):基类名(参数表)
{
//派生类新增成员的初始化语句
}
派生类析构函数
例:
class B
{
public:
B() { cout<<"B()"<<endl; }
~B() { cout<<"~B()"<<endl; }
};
class D : public B
{
public:
D(){ cout<<"D()"<<endl; }
~D() { cout<<"~D()"<<endl; }
};
int main()
{
D d;
return 0;
}
多继承
一个类有多个直接基类的继承关系称为多继承
例:
class Base1
{ public:
Base1(int x) { value = x ; }
int getData() const { return value ; }
protected:
int value;
};
class Base2
{ public:
Base2(char c) { letter=c; }
char getData() const { return letter;}
protected:
char letter;
};
class Derived : public Base1, public Base2
{ friend ostream &operator<< ( ostream &, const Derived & ) ;
public :
Derived ( int, char, double ) ;
double getReal() const ;
private :
double real ;
};
多继承方式下构造函数执行顺序:
1.先执行所有基类的构造函数
2.再执行对象成员的构造函数
3.最后执行派生类的构造函数
虚基类
class B { public : int b ;} ;
class B1 : public B { private : int b1 ; } ;
class B2 : public B { private : int b2 ; } ;
class C : public B1 , public B2
{ public : int f ( ) ; private : int d ; } ;
#include<iostream>
using namespace std ;
class A
{ public :
A ( ) { cout << "class A" << endl ; }
} ;
class B : public A
{ public :
B ( ) {cout << "class B" << endl ; }
} ;
class C : public A
{ public :
C ( ) {cout << "class C" << endl ; }
} ;
class D : public B , public C
{ public :
D ( ) {cout << "class D" << endl ; }
} ;
int main ( )
{ D dd ; }