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

C++面向对象

2010-02-24 20:22 190 查看
1.        类的实现实际上是类的成员函数的实现,即定义类的成员函数。成员函数的定义形式与一般的函数的定义基本相同,但如果在类的外部定义成员函数,必须在成员函数名前加上类名和作用域限定符(::)。            一般将类的定义放在头文件(*.h)中,类的实现放在源文件(*.cpp)中,而main 函数可以放在另一个源文件中。            声明对象后,就可以像引用结果变量一样,利用成员运算符“.”或指向运算符“->”引用对象的公有成员,但注意不能引用对象的非公有成员。

2.        C++中结构体可以包含函数,而在C语言中不能。因此可以将成员函数封装在结构体中。

3.        struct的数据成员和成员函数在缺省状态下是公有的,而类class在缺省状态下是私有的。

4.        构造函数最重要的作用是创建对象本身。申明对象时自动调用构造函数。C+规定,每个类必须有一个构造函数,没有构造函数,就不能创建任何对象。如果一个类没有提供任何的构造函数,则提供一个默认的构造函数(由C++编译器提供),这个默认的构造函数不带参数,只负责创建对象,而不做任何初始化工作。若定义了一个构造函数,C++就不再提供默认构造函数。

5.        析构函数,当一个对象生命周期结束时,进行对象内存的回收。析构函数不允许带参数,一个类中只能有一个析构函数。

6.        函数的重载 重载够成的条件:函数的参数个数,参数类型不同。
#include<iostream.h>
class Point
{
public:
int x;
int y;

Point()
{
x=0;
y=0;
}
Point(int a,int b)
{
x=a;
y=b;
}
~Point()
{
}
void output()
{
cout<<x<<endl<<y<<endl;
}
void output(int x,int y)
{
x=x;               //变量可见性问题,未对变量赋值
y=y;
}

};
void main()
{
Point pt(3,3);
pt.output(5,5);
pt.output();
}


       输出结果为:3 3
若将x=x和y=y换成    this->x=x;和this->y=y;
则输出结果为5 5 。

7.        this指针是一个隐含的指针,它是指向对象本身,代表了对象指针。在上题中,在对象调用pt.output(5,5)时,成员函数除了接受2个实参外,还接收到了一个对象s的地址。这个地址被一个隐含的形参this指针获取,他等同于执行this=&pt。

8.        类的继承       对于子类来说,可以继承父类保护的方法,但此保护的方法在外部是不能被访问的。父类是私有的那么在子类中都不能访问。父类的构造函数比子类的构造函数先调用,析构时则相反。

9.         
[align=center]类的继承访问特性[/align]

基类的访问特性
类的继承特性
子类的访问特性
[align=center]Public[/align]
[align=center]Protected[/align]
[align=center]Private[/align]
[align=center]Public[/align]
[align=center]Public[/align]
[align=center]Protected[/align]
[align=center]No access[/align]
[align=center]Public[/align]
[align=center]Protected[/align]
[align=center]Private[/align]
[align=center]Protected[/align]
[align=center] [/align]
[align=center]Protected[/align]
[align=center]Protected[/align]
[align=center]No access[/align]
[align=center]Public[/align]
[align=center]Protected[/align]
[align=center]Private[/align]
[align=center]Private[/align]
[align=center]Private[/align]
[align=center]Private[/align]
[align=center]No access[/align]
10.    函数的覆盖        发生在父类与子类之间的
#include <iostream.h>
class Animal
{
public:
       void eat()
       {
              cout<<"animal eat"<<endl;
       }
       void sleep()
       {
              cout<<"animal sleep"<<endl;
       }
       void breathe()
       {
              cout<<"animal breathe"<<endl;
       }
};
class Fish:public Animal
{
public:
       void breathe()                     //此时覆盖了父类中的函数void breathe()
       {

//Animal ::breathe();                   //::为作用域标识符,表示函数属于哪一个类的
              cout<<"fish bubble"<<endl;
       }
 
};
void main()
{
       Fish fh;
       fh.breathe();
}
输出结果:fish bubble
若将注释取消,则结果为:animal breathe
                                          fish bubble

11.    多态性、虚函数、纯虚函数
#include <iostream.h>
class Animal
{
public:
       void eat()
       {
              cout<<"animal eat"<<endl;
       }
       void sleep()
       {
              cout<<"animal sleep"<<endl;
       }
       virtual void breathe()                 //加上virtual就使得该函数变成虚函数,若子类中有同名的函数调用时就调用子类中的那个函数,若没有就调用父类中的那个
       {
              cout<<"animal breathe"<<endl;
       }
};
class Fish:public Animal
{
public:
       void breathe()                      //此时覆盖了父类中的函数void breathe()
       {
              cout<<"fish bubble"<<endl;
       }
 
};
void fn(Animal *pAn)
{
       pAn->breathe();
}
void main()
{
       Fish fh;
       Animal *pAn;
       pAn=&fh;
       fn(pAn);
}
输出结果:fish bubble         若取消virtual则输出结果为animal breathe。
 
纯虚函数:
virtual void breathe()=0 将使该函数为纯虚函数。

纯虚函数是一种特殊的虚函数,它没有函数体,,仅提供一个与派生类相一致的接口。

一般情况下,基类和派生类都可以用来声明对象,但如果需要,可以基类作为纯虚函数的一种抽象,即它的一些行为不给出具体的定义,这样的类就被称为抽象类。显然,抽象类至少带有一个纯虚函数。

抽象类不能用来声明对象,只能作为基类来使用,因此它又被称为抽象基类。

12.    引用,相当于给变量起了一个别名。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息