2009年5月21号
VC++学习笔记(一) —— C++经典语法知识
声明:此方法只是用于本人所好,对您带来的不便,敬请见谅
C++经典语法知识学习
======================我是分割线==============================
#include <iostream.h> class Animal{ public: //对于构造函数,父子类的关系是: //先构造父类,再构造子类,也就是,先打印“Animal constructor”再打印“Fish constructor” //要先有父亲,再有孩子 /* Animal(){ cout<<"Animal constructor"<<endl; } */ Animal(int weight,int height){ //cout<<"Animal constructor"<<endl; } //但是,对于析构函数,是子类先析构,父类再析构,注意这个区别 ~Animal(){ //cout<<"Animal deconstructor"<<endl; } void eat(){ cout<<"Animal eat"<<endl; } //sleep()和breathe()两个方法是受保护的,而eat()方法是public protected://受保护的方法,在子类中是可以访问得到的,但这些方法在外部是不可以被访问的 void sleep(){ cout<<"Animal sleep"<<endl; } //private: public : //虚拟函数关键字,如果方法前加上virtual, //表示此方法是虚函数,那么将不再调用Animal的breathe方法了 //如果子类中没有此方法,那么还是会调用基类中的此方法的,当然前提是,传入的是子类的地址 //这里体现了C++的多态性 virtual void breathe(){ cout<<"Animal breathe"<<endl; } // 纯虚函数:没有函数体的函数 //virtual void breathe()=0; //含有纯虚函数的泪叫做抽象类,抽象类是不能实例化对象的 void run();//函数的原形的声明 //注意一定要写分号结尾“:”,这点与JAVA不同 }; //函数的实现 //将函数的实现拿到类的定义之外的方法 void Animal::run(){ } /* 原来的写法 class Fish{ public: void eat(){ cout<<"Fish eat"<<endl; } void sleep(){ cout<<"Fish sleep"<<endl; } void breathe(){ cout<<"Fish breathe"<<endl; } }; */ //c++中的继承,面向对象的机制 //Fish叫做派生类,从Animal类派生出来的一个类 class Fish : public Animal{ //任何父类有的方法,子类中都不需要再重复的写一遍 public: //如果没有后面的带参的父类的构造函数,程序会报错的 //---------------------常量的初始化 Fish():Animal(400,300),a(1){ //cout<<"Fish constructor"<<endl; } ~Fish(){ //cout<<"Fish deconstructor"<<endl; } void test(){ sleep(); //如果父类中是protected修饰的,则这里可以被访问, //但是,父类中是private修饰的,所以,这里也不可以访问 //breathe();//error } //函数的覆盖:发生在父类与子类之间 void breathe(){ //如果也要调用父类的这个函数 //----作用域标识符 //Animal::breathe(); cout<<"Fish breathe"<<endl; } private: const int a; }; //全局函数 void fn(Animal *pAn){ pAn->breathe(); } //引用通常用作参数传递 int change(int &a,int &b){ //交换a和b的值的操作 return 0 ; } void main(){ //Animal an; //an.eat(); //an.sleep();//error //an.breathe();//error,同样的道理 Fish fish; //fish.sleep();//error,这个方法是protected的,在外部是不能被访问的 //fish.test();//ok,在子类中,protected是可以被访问的 //fish.breathe(); Animal *pAn;//定义一个Animal的指针 pAn=&fish;//将Fish实例的地址赋给Animal的指针 fn(pAn);//调用的是Animal的breathe方法 //但是注意,如果基类中的函数添加上了virtual关键字,表示虚函数是,将调用的是子类的breathe方法 //引用的应用 //应用相当于变量的别名 int x = 3; int y = 4; change(x,y); }
======================我是分割线==============================
#include "Animal.h" #include <iostream.h> Animal::Animal(int weight,int height){ cout<<"Animal constructor"<<endl; } void Animal::breathe(){ cout<<"Animal breathe"<<endl; } void Animal::eat(){ cout<<"Animal eat"<<endl; } void Animal::run(){ cout<<"Animal run"<<endl; } //void Animal::sleep(){} //void Animal::~Animal(){}
======================我是分割线==============================
#ifndef ANIMAL_H_H #define ANIMAL_H_H //预定义指令符,防止多次包含头文件的情况 class Animal{ public: //对于构造函数,父子类的关系是: //先构造父类,再构造子类,也就是,先打印“Animal constructor”再打印“Fish constructor” //要先有父亲,再有孩子 /* Animal(){ cout<<"Animal constructor"<<endl; } */ Animal(int weight,int height); //但是,对于析构函数,是子类先析构,父类再析构,注意这个区别 //~Animal(); void eat(); //sleep()和breathe()两个方法是受保护的,而eat()方法是public protected://受保护的方法,在子类中是可以访问得到的,但这些方法在外部是不可以被访问的 void sleep(); //private: public : //虚拟函数关键字,如果方法前加上virtual, //表示此方法是虚函数,那么将不再调用Animal的breathe方法了 //如果子类中没有此方法,那么还是会调用基类中的此方法的,当然前提是,传入的是子类的地址 //这里体现了C++的多态性 virtual void breathe(); // 纯虚函数:没有函数体的函数 //virtual void breathe()=0; //含有纯虚函数的泪叫做抽象类,抽象类是不能实例化对象的 void run();//函数的原形的声明 //注意一定要写分号结尾“:”,这点与JAVA不同 }; #endif
======================我是分割线==============================
#include "Animal.h" #ifndef FISH_H_H #define FISH_H_H //c++中的继承,面向对象的机制 //Fish叫做派生类,从Animal类派生出来的一个类 class Fish : public Animal{ //任何父类有的方法,子类中都不需要再重复的写一遍 public: //如果没有后面的带参的父类的构造函数,程序会报错的 //---------------------常量的初始化 Fish(); //函数的覆盖:发生在父类与子类之间 void breathe(); }; #endif
======================我是分割线==============================
#include "Fish.h" #include <iostream.h> Fish::Fish():Animal(300,400){ cout<<"Fish constructor"<<endl; } void Fish::breathe(){ cout<<"Fish breathe"<<endl; } //Fish::sleep(){} //Fish::~Fish(){}
======================我是分割线==============================
#include <iostream.h> #include "Animal.h" #include "Fish.h" //全局函数 void fn(Animal *pAn){ pAn->breathe(); } //引用通常用作参数传递 int change(int &a,int &b){ //交换a和b的值的操作 return 0 ; } void main(){ //Animal an; //an.eat(); //an.sleep();//error //an.breathe();//error,同样的道理 Fish fish; //fish.sleep();//error,这个方法是protected的,在外部是不能被访问的 //fish.test();//ok,在子类中,protected是可以被访问的 //fish.breathe(); Animal *pAn;//定义一个Animal的指针 pAn=&fish;//将Fish实例的地址赋给Animal的指针 fn(pAn);//调用的是Animal的breathe方法 //但是注意,如果基类中的函数添加上了virtual关键字,表示虚函数是,将调用的是子类的breathe方法 //引用的应用 //应用相当于变量的别名 int x = 3; int y = 4; change(x,y); }
======================我是分割线==============================
