C++基础篇
2015-11-18 12:32
441 查看
================== 第一部分 ==============================
[cpp]
view plaincopy
#include <iostream.h>
//////////////////////////////////////////////////////////////////////////
// 基类(父类)
class Animal
{
public:
Animal()
{
cout << "animal 结构体" << endl ; // 第一
}
~Animal()
{
cout << "animal 析构函数" << endl; // 第四
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 派生类(子类)
class Fish : public Animal
{
public:
Fish()
{
cout << "Fish 结构体" << endl; // 第二
}
~Fish()
{
cout << "Fish 析构函数" << endl; // 第三
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 主函数
int main()
{
Fish fs;
return 0;
}
输出结果:
animal 结构体
Fish 结构体
Fish 析构函数
animal 析构函数
流程:先执行 父类构造函数→子类构造函数 → 子类析构函数 → 父类析构函数。
======================================= 第二部分:函数重载(发生内部类) ======================================
函数重载构成条件:1、函数的参数类型不能相同; 2、函数的参数个数不能相同。
[cpp]
view plaincopy
class Test
{
int x,y;
public:
Test()
{
x = 10;
y = 20;
}
void sum()
{
cout << this->x + this->y << endl;
}
void sum(int x, int y)
{
this->x = x;
this->y = y;
cout << this->x + this->y << endl;
}
};
//////////////////////////////////////////////////////////////////////////
// 主函数
int main()
{
Test test;
test.sum(); // 重载 不带参数
test.sum(8,3); // 重载带参数
return 0;
}
注意:以下两种情况不能够构成函数重载:
1、void add();
int add();
2、 int add( int a , int b = 5 );
int add( int a);
======================================= 第三部分:函数覆盖(发生在父类与子类之间)=======================================
函数覆盖:子类与父类方法名称相同。
好处:当子类要特有自己的行为时候,可以通过函数覆盖来得到。
// 基类(父类)
[cpp]
view plaincopy
class Animal
{
public:
Animal()
{
}
void breathe()
{
cout << "animal 呼吸方法" << endl;
}
~Animal()
{
} };
//////////////////////////////////////////////////////////////////////////
// 派生类(子类)
class Fish : public Animal
{
public:
Fish()
{
}
void breathe()
{
cout << "Fish breathe() 呼吸方法" << endl;
}
~Fish()
{
}
};//////////////////////////////////////////////////////////////////////////
// 主函数
int main()
{
Fish fs; // 构造 Fish 对象
fs.breathe(); // 执行子类的 方法
return 0;
}
输出结果: Fish breathe() 呼吸方法
======================================= 第四部分:多态性 =======================================
[cpp]
view plaincopy
#include <iostream.h>
//////////////////////////////////////////////////////////////////////////
// 基类(父类)
class Animal
{
public:
Animal(int hight , int weight)
{
}
void breathe()
{
cout << "animal 呼吸方法" << endl;
}
~Animal()
{
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 派生类(子类)
class Fish : public Animal
{
public:
Fish():Animal(100,200)
{
}
void breathe()
{
cout << "Fish breathe() 呼吸方法" << endl;
}
~Fish()
{
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 主函数
int main()
{
Fish fs; // 构造 Fish 对象
Animal *pAnimal; // 构造Animal指针
pAnimal = &fs; // Fish对象地址赋给pAnimal
pAnimal->breathe();
return 0;
}
输出结果:animal 呼吸方法
1.为什么输出的结果不是 Fish breathe() 呼吸方法 ?
Fish 和 Animal 首地址是一样的,都是Animal 对象内存地址。
Fish 对象内存布局如下:
•
Animal 对象内存
Fish 继承部分
2. 假如想要输出 Fish 对象的呼吸方法怎么办?
可以在父类呼吸方法加上关键字 virtual 。
// 基类(父类)
[cpp]
view plaincopy
class Animal
{
.... // 这里加上 关键字 virtual
virtual void breathe()
{
cout << "animal 呼吸方法" << endl;
}
....};
此时输出结果是:Fish breathe() 呼吸方法。
多态性总结:当基类函数是虚函数时,如果子类有基类的方法,则调用子类的方法;如果子类没有基类的方法,则调用父类的方法。
===================================== 第五部分:抽象(纯虚函数) =====================================
如果一个类中至少有一个纯虚函数,那么这个类叫做抽象类(abstract)。
抽象类不能够实例化对象:
// 基类(父类)
[cpp]
view plaincopy
class Animal
{
public:
Animal()
{
}
virtual void breathe() = 0 ;
~Animal()
{
}
};
// 派生类(子类)
class Fish : public Animal
{
public:
Fish()
{
} ~Fish()
{
}
};
// 主函数
int main()
{
Animal animal; // 不能实例化对象
[cpp]
view plaincopy
Fish fs; // 同样不能实例化对象。因为 子类Fish 是从 父类Animal 继承而来,所以Fsih 也变成了抽象类。
[cpp]
view plaincopy
return 0;
}
编译错误提示:cannot instantiate abstract class due to following members:
要想子类Fish 不变成抽象类,子类就要把 父类的纯虚函数breathe() 实现。
子类改为:
// 派生类(子类)
class Fish : public Animal
{
public:
Fish()
{
}
void breathe()
{
cout << "Fish breathe() 呼吸方法" << endl;
}
~Fish()
{
}
};
===================================== 第六部分: 带参数的基类(父类)构造函数 =======================================
[cpp]
view plaincopy
#include <iostream.h>
//////////////////////////////////////////////////////////////////////////
// 基类(父类)
class Animal
{
public:
Animal(int hight , int weight)
{
cout << "animal 结构体" << endl ; // 第一
}
void breathe()
{
cout << "animal 呼吸方法" << endl;
}
~Animal()
{
cout << "animal 析构函数" << endl; // 第四
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 派生类(子类)
class Fish : public Animal
{
public:
Fish()
{
cout << "Fish 结构体" << endl; // 第二
}
void breathe()
{
cout << "Fish breathe() 呼吸方法" << endl;
}
~Fish()
{
cout << "Fish 析构函数" << endl; // 第三
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 主函数
int main()
{
Fish fs;
return 0;
}
编译提示:'Animal' : no appropriate default constructor available。(没有适合的默认构造函数)
更正如下:
//////////////////////////////////////////////////////////////////////////
// 派生类(子类)
[cpp]
view plaincopy
class Fish : public Animal
{
public: // 子类 向 基类的带参数构造函数 传递参数。 // 当构造 Fish 对象时候,会调用带参数的Animal(int ,int)。
[cpp]
view plaincopy
Fish():Animal(100,200)
{ // 注意:还没有进入到这里之前先调用 Animal
cout << "Fish 结构体" << endl; // 第二
}
void breathe()
{
cout << "Fish breathe() 呼吸方法" << endl;
}
~Fish()
{
cout << "Fish 析构函数" << endl; // 第三
}
protected:
private:
};
输出结果:(同上)
animal 结构体
Fish 结构体
Fish 析构函数
animal 析构函数
=====================================第五部分:类常量成员初始化=======================
1、第一种:
[cpp]
view plaincopy
class Test
{
public:
int sum;
public:
Test()
{
this.sum = 10; // 初始化 成员变量
}
};
第二种:
class Test
{
public:
int sum;// 定义变量public:
Test():sum(10) // 初始化 成员变量
{
}
};
另外:
静态成员比较特殊,可以直接在类外面初始化,但是要指明其域。
[cpp]
view plaincopy
class Test
{
public:
static const int sum;
public:
Test()
{
cout << this.sum<<endl; // 输出 88
}
};
const int Test::sum = 88; // 初始化 静态类成员
[cpp]
view plaincopy
#include <iostream.h>
//////////////////////////////////////////////////////////////////////////
// 基类(父类)
class Animal
{
public:
Animal()
{
cout << "animal 结构体" << endl ; // 第一
}
~Animal()
{
cout << "animal 析构函数" << endl; // 第四
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 派生类(子类)
class Fish : public Animal
{
public:
Fish()
{
cout << "Fish 结构体" << endl; // 第二
}
~Fish()
{
cout << "Fish 析构函数" << endl; // 第三
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 主函数
int main()
{
Fish fs;
return 0;
}
输出结果:
animal 结构体
Fish 结构体
Fish 析构函数
animal 析构函数
流程:先执行 父类构造函数→子类构造函数 → 子类析构函数 → 父类析构函数。
======================================= 第二部分:函数重载(发生内部类) ======================================
函数重载构成条件:1、函数的参数类型不能相同; 2、函数的参数个数不能相同。
[cpp]
view plaincopy
class Test
{
int x,y;
public:
Test()
{
x = 10;
y = 20;
}
void sum()
{
cout << this->x + this->y << endl;
}
void sum(int x, int y)
{
this->x = x;
this->y = y;
cout << this->x + this->y << endl;
}
};
//////////////////////////////////////////////////////////////////////////
// 主函数
int main()
{
Test test;
test.sum(); // 重载 不带参数
test.sum(8,3); // 重载带参数
return 0;
}
注意:以下两种情况不能够构成函数重载:
1、void add();
int add();
2、 int add( int a , int b = 5 );
int add( int a);
======================================= 第三部分:函数覆盖(发生在父类与子类之间)=======================================
函数覆盖:子类与父类方法名称相同。
好处:当子类要特有自己的行为时候,可以通过函数覆盖来得到。
// 基类(父类)
[cpp]
view plaincopy
class Animal
{
public:
Animal()
{
}
void breathe()
{
cout << "animal 呼吸方法" << endl;
}
~Animal()
{
} };
//////////////////////////////////////////////////////////////////////////
// 派生类(子类)
class Fish : public Animal
{
public:
Fish()
{
}
void breathe()
{
cout << "Fish breathe() 呼吸方法" << endl;
}
~Fish()
{
}
};//////////////////////////////////////////////////////////////////////////
// 主函数
int main()
{
Fish fs; // 构造 Fish 对象
fs.breathe(); // 执行子类的 方法
return 0;
}
输出结果: Fish breathe() 呼吸方法
======================================= 第四部分:多态性 =======================================
[cpp]
view plaincopy
#include <iostream.h>
//////////////////////////////////////////////////////////////////////////
// 基类(父类)
class Animal
{
public:
Animal(int hight , int weight)
{
}
void breathe()
{
cout << "animal 呼吸方法" << endl;
}
~Animal()
{
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 派生类(子类)
class Fish : public Animal
{
public:
Fish():Animal(100,200)
{
}
void breathe()
{
cout << "Fish breathe() 呼吸方法" << endl;
}
~Fish()
{
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 主函数
int main()
{
Fish fs; // 构造 Fish 对象
Animal *pAnimal; // 构造Animal指针
pAnimal = &fs; // Fish对象地址赋给pAnimal
pAnimal->breathe();
return 0;
}
输出结果:animal 呼吸方法
1.为什么输出的结果不是 Fish breathe() 呼吸方法 ?
Fish 和 Animal 首地址是一样的,都是Animal 对象内存地址。
Fish 对象内存布局如下:
•
Animal 对象内存
Fish 继承部分
2. 假如想要输出 Fish 对象的呼吸方法怎么办?
可以在父类呼吸方法加上关键字 virtual 。
// 基类(父类)
[cpp]
view plaincopy
class Animal
{
.... // 这里加上 关键字 virtual
virtual void breathe()
{
cout << "animal 呼吸方法" << endl;
}
....};
此时输出结果是:Fish breathe() 呼吸方法。
多态性总结:当基类函数是虚函数时,如果子类有基类的方法,则调用子类的方法;如果子类没有基类的方法,则调用父类的方法。
===================================== 第五部分:抽象(纯虚函数) =====================================
如果一个类中至少有一个纯虚函数,那么这个类叫做抽象类(abstract)。
抽象类不能够实例化对象:
// 基类(父类)
[cpp]
view plaincopy
class Animal
{
public:
Animal()
{
}
virtual void breathe() = 0 ;
~Animal()
{
}
};
// 派生类(子类)
class Fish : public Animal
{
public:
Fish()
{
} ~Fish()
{
}
};
// 主函数
int main()
{
Animal animal; // 不能实例化对象
[cpp]
view plaincopy
Fish fs; // 同样不能实例化对象。因为 子类Fish 是从 父类Animal 继承而来,所以Fsih 也变成了抽象类。
[cpp]
view plaincopy
return 0;
}
编译错误提示:cannot instantiate abstract class due to following members:
要想子类Fish 不变成抽象类,子类就要把 父类的纯虚函数breathe() 实现。
子类改为:
// 派生类(子类)
class Fish : public Animal
{
public:
Fish()
{
}
void breathe()
{
cout << "Fish breathe() 呼吸方法" << endl;
}
~Fish()
{
}
};
===================================== 第六部分: 带参数的基类(父类)构造函数 =======================================
[cpp]
view plaincopy
#include <iostream.h>
//////////////////////////////////////////////////////////////////////////
// 基类(父类)
class Animal
{
public:
Animal(int hight , int weight)
{
cout << "animal 结构体" << endl ; // 第一
}
void breathe()
{
cout << "animal 呼吸方法" << endl;
}
~Animal()
{
cout << "animal 析构函数" << endl; // 第四
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 派生类(子类)
class Fish : public Animal
{
public:
Fish()
{
cout << "Fish 结构体" << endl; // 第二
}
void breathe()
{
cout << "Fish breathe() 呼吸方法" << endl;
}
~Fish()
{
cout << "Fish 析构函数" << endl; // 第三
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
// 主函数
int main()
{
Fish fs;
return 0;
}
编译提示:'Animal' : no appropriate default constructor available。(没有适合的默认构造函数)
更正如下:
//////////////////////////////////////////////////////////////////////////
// 派生类(子类)
[cpp]
view plaincopy
class Fish : public Animal
{
public: // 子类 向 基类的带参数构造函数 传递参数。 // 当构造 Fish 对象时候,会调用带参数的Animal(int ,int)。
[cpp]
view plaincopy
Fish():Animal(100,200)
{ // 注意:还没有进入到这里之前先调用 Animal
cout << "Fish 结构体" << endl; // 第二
}
void breathe()
{
cout << "Fish breathe() 呼吸方法" << endl;
}
~Fish()
{
cout << "Fish 析构函数" << endl; // 第三
}
protected:
private:
};
输出结果:(同上)
animal 结构体
Fish 结构体
Fish 析构函数
animal 析构函数
=====================================第五部分:类常量成员初始化=======================
1、第一种:
[cpp]
view plaincopy
class Test
{
public:
int sum;
public:
Test()
{
this.sum = 10; // 初始化 成员变量
}
};
第二种:
class Test
{
public:
int sum;// 定义变量public:
Test():sum(10) // 初始化 成员变量
{
}
};
另外:
静态成员比较特殊,可以直接在类外面初始化,但是要指明其域。
[cpp]
view plaincopy
class Test
{
public:
static const int sum;
public:
Test()
{
cout << this.sum<<endl; // 输出 88
}
};
const int Test::sum = 88; // 初始化 静态类成员
相关文章推荐
- 一道关于C++虚函数和多继承的面试题
- c++ java
- C++调用Tesseract
- 剑指offer第三十二题【把数组排成最小的数】c++实现
- C++ 对象的内存布局
- 约瑟夫环-顺序表-C++
- 腾讯c++ 阿里java
- 小顶堆第二弹-----堆降序排序(C语言非递归)
- C++编程规范---读书有感(2)
- C语言——符号的声明与定义
- C++类中的枚举类型
- C语言---迷宫游戏 代码
- C++中的单例模式其实也不简单
- C++文件 读写操作大全
- 剑指offer第三十一题【整数中1出现的次数(从1到n整数中1出现的次数)】c++实现
- C语言之动态内存分配
- C++的虚函数表
- C++输入输出操作符重载
- C++输入输出操作符重载
- 【c++】公司职员系统