zoukankan      html  css  js  c++  java
  • 孙鑫vc++学习(vs2008)笔记之第二课掌握C++

    View Code
     1 #include <iostream>
    2
    3 using namespace std;
    4
    5 class Animal //基类
    6 {
    7 public:
    8 Animal(int height,int width)
    9 {
    10 cout << "Animal construct" << endl;
    11 }
    12 ~Animal()
    13 {
    14 cout << "animal deconstruct" << endl;
    15 }
    16 void eat()
    17 {
    18 cout << "animal eat" << endl;
    19 }
    20 //protected: //子类中可以访问,但外部不能访问
    21 void sleep()
    22 {
    23 cout << "animal sleep" << endl;
    24 }
    25 //private: //子类都不能访问
    26 virtual void breathe() //=0 //虚函数,多态性(纯虚函数)
    27 {
    28 cout << "animal breathe" << endl;
    29 }
    30
    31 };
    32
    33 class Fish : public Animal //派生类
    34 {
    35 public:
    36 Fish():Animal(400,500),a(1) //常量初始化
    37 {
    38 cout << "Fish construct" << endl;
    39 }
    40 ~Fish()
    41 {
    42 cout << "fish deconstruct" << endl;
    43 }
    44 void breathe() //函数覆盖
    45 {
    46 Animal::breathe(); //复古
    47 cout << "fish bubble" << endl;
    48 }
    49 void test()
    50 {
    51 sleep();
    52 breathe();
    53 }
    54 private:
    55 const int a;
    56
    57 };
    58
    59 void fn(Animal *pAn)
    60 {
    61 pAn->breathe();
    62 }
    63
    64 int change(int &a,int &b) //引用
    65 {
    66
    67 }
    68
    69 void main()
    70 {
    71 // Animal an;
    72 // an.eat();
    73 // an.breathe();
    74 Fish fh;
    75 Animal *pAn;
    76 pAn=&fh;
    77 fn(pAn);
    78 // fh.test(); //先构造父类,先析构子类
    79 getchar();
    80 }

    1.struct所有的成员函数缺省情况下都是public,而class是private

    2.类的实例和对象 可以等同

    3.构造函数:最重要的作用是创建对象本身,c++规定每一个类必须有一个构造函数,如果没有编译器会提供一个默认的构造函数。

    4.析构函数:内存回收,对象释放。析构函数不能有参数,并且一个类中只能有一个析构函数

    5.函数重载:构成条件:函数的参数类型,参数个数不同。

    6.this指针:是一个隐含的指针,它是指向对象本身,代表了对象的地址。等同于this=&pt

    小技巧:this-> 可以自动列出类的成员。

    7.类的继承

     

     

     

    8.函数的覆盖:发生在父类和子类之间

    9. protected:子类中可以访问,但外部不能访问,private:子类也不可以访问

    类型转换(补充):看内存模型是否匹配

    10.c++多态性,迟绑定(late binding)的技术,在运行时,依据对象的类型来确认调用的是哪个函数。

    11.纯虚函数:virtual fn() = 0。抽象类,不能定义对象,除非派生类中把类具体实现。用于设计基类。

    12.引用:int &b = a;通常用于函数传参。引用和指针,指针本身需要内存,引用不需要

    13.使用预编译指令避免重复包含头文件。

    #ifndef FISH_H_H //预编译指令符避免头文件重复包含

    #define FISH_H_H

    #endif

    14.小技巧:ctrl+F7单独编译一个cpp文件,头文件不参与编译

     

  • 相关阅读:
    技术分享:unity3d基础问题
    转载》【3个免费名额】爱加密TFC大会15万手游保护服务免费送!速来报名!
    Android APP通用型拒绝服务、漏洞分析报告
    493万Gmail用户的账号密码遭泄露,疑从其他网站数据库提炼而成
    转载:在 android dialog中使用Autocompletetext
    转载 iOS工程适配64-bit 经验分享
    爱加密联合应用之星(APPSTAR)为开发者提供免费云加密服务
    魔学院_部门课程笔记1_LoadRunner性能测试
    Jmeter学习笔记12-监听器以及测试结果的分析
    Jmeter学习笔记11-分布式性能测试
  • 原文地址:https://www.cnblogs.com/quantumplan/p/2348040.html
Copyright © 2011-2022 走看看