c++考试总结

来源:互联网 发布:返利商城APP源码 编辑:程序博客网 时间:2024/05/19 19:34

对象 - 对象具有状态和行为
类 - 类可以定义为描述对象行为/状态的模板/蓝图。
方法 - 从基本上说,一个方法表示一种行为。一个类可以包含多个方法

char 1 个字节
int 4 个字节
float 4 个字节
double 8 个字节

全局变量可以被任何函数访问。也就是说,全局变量一旦声明,在整个程序中都是可用的

static 存储类:static 存储类指示编译器在程序的生命周期内保持局部变量的存在,而不需要在每次它进入和离开作用域时进行创建和销毁。因此,使用 static 修饰局部变量可以在函数调用之间保持局部变量的值。
例子:

#include <iostream>// 函数声明void func(void);static int count = 10; /* 全局变量 */int main(){    while(count--)    {       func();    }    return 0;}// 函数定义void func( void ){    static int i = 5; // 局部静态变量    i++;    std::cout << "变量 i 为 " << i ;    std::cout << " , 变量 count 为 " << count << std::endl;}

运行结果:

变量 i 为 6 , 变量 count9变量 i 为 7 , 变量 count8变量 i 为 8 , 变量 count7变量 i 为 9 , 变量 count6变量 i 为 10 , 变量 count5变量 i 为 11 , 变量 count4变量 i 为 12 , 变量 count3变量 i 为 13 , 变量 count2变量 i 为 14 , 变量 count1变量 i 为 15 , 变量 count0

C++ 引用: 引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字。一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量。

#include <iostream>using namespace std;int main (){   // 声明简单的变量   int    i;   double d;   // 声明引用变量   int&    r = i;   double& s = d;   i = 5;   cout << "Value of i : " << i << endl;   cout << "Value of i reference : " << r  << endl;   d = 11.7;   cout << "Value of d : " << d << endl;   cout << "Value of d reference : " << s  << endl;   return 0;}

运行结果:

Value of i : 5Value of i reference : 5Value of d : 11.7Value of d reference : 11.7

一个类可以派生自多个类,这意味着,它可以从多个基类继承数据和函数。

#include <iostream>using namespace std;// 基类class Shape{   public:      void setWidth(int w)      {         width = w;      }      void setHeight(int h)      {         height = h;      }   protected:      int width;      int height;};// 派生类class Rectangle: public Shape{   public:      int getArea()      {         return (width * height);      }};int main(void){   Rectangle Rect;   Rect.setWidth(5);   Rect.setHeight(7);   // 输出对象的面积   cout << "Total area: " << Rect.getArea() << endl;   return 0;}

运行结果:

Total area: 35

派生类可以访问基类中所有的非私有成员。因此基类成员如果不想被派生类的成员函数访问,则应在基类中声明为 private。
我们可以根据访问权限总结出不同的访问类型,如下所示:

访问 public protected private
同一个类 yes yes yes
派生类 yes yes no
外部的类 yes no no

多继承:C++ 类可以从多个类继承成员,语法如下:

class <派生类名>:<继承方式1><基类名1>,<继承方式2><基类名2>,…{<派生类类体>};

C++ 中的运算符重载:重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。
声明加法运算符用于把两个 Box 对象相加,返回最终的 Box 对象。大多数的重载运算符可被定义为普通的非成员函数或这被定义为类成员函数。如果我们定义上面的函数为类的非成员函数,那么我们需要为每次操作传递两个参数,如下所示:

Box operator+(const Box&, const Box&);

在这里,对象作为参数进行传递,对象的属性使用 this 运算符进行访问,如下所示:

#include <iostream>using namespace std;class Box{   public:      double getVolume(void)      {         return length * breadth * height;      }      void setLength( double len )      {          length = len;      }      void setBreadth( double bre )      {          breadth = bre;      }      void setHeight( double hei )      {          height = hei;      }      // 重载 + 运算符,用于把两个 Box 对象相加      Box operator+(const Box& b)      {         Box box;         box.length = this->length + b.length;         box.breadth = this->breadth + b.breadth;         box.height = this->height + b.height;         return box;      }   private:      double length;      // 长度      double breadth;     // 宽度      double height;      // 高度};// 程序的主函数int main( ){   Box Box1;                // 声明 Box1,类型为 Box   Box Box2;                // 声明 Box2,类型为 Box   Box Box3;                // 声明 Box3,类型为 Box   double volume = 0.0;     // 把体积存储在该变量中   // Box1 详述   Box1.setLength(6.0);    Box1.setBreadth(7.0);    Box1.setHeight(5.0);   // Box2 详述   Box2.setLength(12.0);    Box2.setBreadth(13.0);    Box2.setHeight(10.0);   // Box1 的体积   volume = Box1.getVolume();   cout << "Volume of Box1 : " << volume <<endl;   // Box2 的体积   volume = Box2.getVolume();   cout << "Volume of Box2 : " << volume <<endl;   // 把两个对象相加,得到 Box3   Box3 = Box1 + Box2;   // Box3 的体积   volume = Box3.getVolume();   cout << "Volume of Box3 : " << volume <<endl;   return 0;}

运行结果:

Volume of Box1 : 210Volume of Box2 : 1560Volume of Box3 : 5400

C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。
例子:

#include <iostream> using namespace std;class Shape {   protected:      int width, height;   public:      Shape( int a=0, int b=0)      {         width = a;         height = b;      }      int area()      {         cout << "Parent class area :" <<endl;         return 0;      }};class Rectangle: public Shape{   public:      Rectangle( int a=0, int b=0):Shape(a, b) { }      int area ()      {          cout << "Rectangle class area :" <<endl;         return (width * height);       }};class Triangle: public Shape{   public:      Triangle( int a=0, int b=0):Shape(a, b) { }      int area ()      {          cout << "Triangle class area :" <<endl;         return (width * height / 2);       }};// 程序的主函数int main( ){   Shape *shape;   Rectangle rec(10,7);   Triangle  tri(10,5);   // 存储矩形的地址   shape = &rec;   // 调用矩形的求面积函数 area   shape->area();   // 存储三角形的地址   shape = &tri;   // 调用三角形的求面积函数 area   shape->area();   return 0;}

运行结果:

Parent class areaParent class area

导致错误输出的原因是,调用函数 area() 被编译器设置为基类中的版本,这就是所谓的静态多态,或静态链接 - 函数调用在程序执行前就准备好了。有时候这也被称为早绑定,因为 area() 函数在程序编译期间就已经设置好了。

现在,让我们对程序稍作修改,在 Shape 类中,area() 的声明前放置关键字 virtual,如下所示:

class Shape {   protected:      int width, height;   public:      Shape( int a=0, int b=0)      {         width = a;         height = b;      }      virtual int area()      {         cout << "Parent class area :" <<endl;         return 0;      }};

运行结果:

Rectangle class areaTriangle class area

虚函数:虚函数 是在基类中使用关键字 virtual 声明的函数。在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。
我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数,这种操作被称为动态链接,或后期绑定。

纯虚函数
您可能想要在基类中定义虚函数,以便在派生类中重新定义该函数更好地适用于对象,但是您在基类中又不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。我们可以把基类中的虚函数 area() 改写如下:

class Shape {   protected:      int width, height;   public:      Shape( int a=0, int b=0)      {         width = a;         height = b;      }      // pure virtual function      virtual int area() = 0;};

= 0 告诉编译器,函数没有主体,上面的虚函数是纯虚函数.
如果类中至少有一个函数被声明为纯虚函数,则这个类就是抽象类

原创粉丝点击