Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12403161
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类:

2012-11-10 10:27:35

一、案例代码及运行效果


  1. // Class_Construct_Seq.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <string>
  6. using namespace std; /* 没有std命名空间,cout,endl被标识为未声明 */

  7. class GrandPa
  8. {
  9.     public:
  10.         /* 注意!如果构造函数GrandPa不放在public区域,会被当成private成员 */
  11.         GrandPa(char* s)
  12.         //GrandPa(string s)
  13.         {
  14.             cout<<"I'm GrandPa,Created Successfully!"<<endl;
  15.             cout<<s<<endl<<endl;
  16.         };
  17.         void DelegateShowText()
  18.         {
  19.             ShowText();
  20.         }

  21.     private:
  22.         void ShowText()
  23.         {
  24.             cout<<"Print in GrandPa"<<endl;
  25.         }
  26. }; /* 注意,类的声明后面还有一个分号*/

  27. class Son
  28. {
  29.     public:
  30.         Son()
  31.         {
  32.             cout<<"I'm Son,Created Successfully!"<<endl<<endl;
  33.         }

  34.         void ShowText()
  35.         {
  36.             cout<<"Print in Son"<<endl;
  37.         }
  38. };

  39. //注意!多继承与父类的构造函数初始化都是使用逗号分开
  40. class GrandSon : public GrandPa,public Son
  41. {
  42.     public:
  43.         GrandSon():GrandPa("Vaule from GrandSon"),Son()
  44.         {
  45.             cout<<"I'm GrandSon,Created Successfully!"<<endl<<endl;
  46.         }

  47.         void ShowText()
  48.         {
  49.             cout<<"Print in GrandSon"<<endl;
  50.         }
  51. };

  52. enum
  53. {
  54.     GRANDPA = 1,
  55.     SON,
  56.     GRANDSON,
  57. };

  58. int main()
  59. {
  60.     cout<<"-------------------------------------------------------"<<endl;
  61.     cout<<"Select to create:"<<endl;
  62.     cout<<"1 GrandPa ;"<<endl;
  63.     cout<<"2 Son ;"<<endl;
  64.     cout<<"3 GrandSon ;"<<endl;
  65.     cout<<"-------------------------------------------------------"<<endl;

  66.     int value = 0;
  67.     cin >> value;

  68.     GrandPa gPa("Value in GrandPa");
  69.     Son s;
  70.     GrandSon gSon;

  71.     switch(value)
  72.     {
  73.         case GRANDPA:
  74.             //gPa.ShowText(); 错误的用法,在main中调用GrandPa的私有成员,不允许!!
  75.             gPa.DelegateShowText(); // 但可调用GrandPa的公有成员
  76.             getchar();
  77.             break;

  78.         case SON:
  79.             s.ShowText();
  80.             getchar();
  81.             break;

  82.         case GRANDSON:
  83.             gSon.ShowText();
  84.             getchar();
  85.             break;

  86.         default:
  87.             break;
  88.     }

  89.     getchar(); /* 用于屏幕输出暂停 */
  90.     return 0;
  91. }

 

输出效果

grandson

图 派生类构造的过程

由上图,派生类的构造过程是先构造完基类,才进行自己构造。

 

二、代码与理论分析


上述CPP代码有几个注意点:


1、using namespace std;  /* std命名空间,包含cout,endl的声明,必须写 */

2、class GrandPa{……};  ===>这里一定要有一个逗号

3、 注意!如果构造函数GrandPa不放在public区域,会被当成private成员
class GrandPa
{
    public:
               GrandPa(char* s)

……

}

4、多继承与派生类构造函数写法

(1)注意!多继承写法,父类间用逗号分开

class GrandSon : public GrandPa,public Son{};

(2)注意!派生类的构造函数需要依此给父类的构造函数传值(针对有参数的基类构造函数,没有参数的基类构造函数可以不写)

public:
        GrandSon():GrandPa("Vaule from GrandSon"),Son()
        {
            cout<<"I'm GrandSon,Created Successfully!"<        }

5、CPP枚举类型的写法:内部有逗号分开,结尾用分号

enum
{
    GRANDPA = 1,
    SON,
    GRANDSON,
};

6、不能通过main()调用GrandPa的私有方法,但可以调用其公有方法

             //gPa.ShowText();  错误的用法,在main中调用GrandPa的私有成员,不允许!!
            gPa.DelegateShowText();   // 但可调用GrandPa的公有成员

7、getchar(); 用于控制台的暂停

8、CPP输出字符串的方法

  1. GrandPa(char* s)
  2.         //GrandPa(string s)
  3.         {
  4.             cout<<"I'm GrandPa,Created Successfully!"<<endl;
  5.             cout<<s<<endl<<endl;
  6.         };


阅读(2339) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~