#include <iostream.h>
#include <stdio.h>
//对于struct其中的成员默认的为 public
struct point
{
int x;
int y;
};
//对于class中的成员默认的为privated
class Point
{
public:
int m;
int n;
void Cout()
{
cout<<m<<endl<<n<<endl;
}
protected:
private:
};
//注意结构体和类的最后要加上一个分号。!!!!!!
void main()
{
//知道如何声明一个对象
point pt;
Point Pt;
Pt.m = 0;
Pt.n = 0;
Pt.Cout();
pt.x = 1;
pt.y = 1;
cout << pt.x <<endl << pt.y <<endl;
}
//C++与c相比有很多优点,主要体现在封装性(encapsulation)
//继承性(Inheritance)和多态性(Polymorphism)。
//封装性把数据与操作数据的函数组织在一起,不仅使程序更加紧凑,并且提高了类内部数据的安全性
//继承性增加了软件的可扩充性级代码的重用性
//多态性使设计程序时可以对问题进行更好的抽象,有利于代码的维护和可重复用
//在c语言中,结构体不能包含函数,在面向对象的程序设计中,对象具有状态(属性)和行为,状态保存在成员变量中
//行为通过成员方法(函数)来实现。
Part 2
#include <iostream.h>
class point
{
public:
int x;
int y;
//int y = 0;在类中定义成员变量时,不能直接给变量赋初值
// 构造函数 与类的名字相同,主要作用是初始化类中的成员变量
//为对象分配内存空间
// 注意构造函数调用的时间
point()
{
x = 0;
y = 0;
}
// 析构函数只能有一个,其主要作用是释放对象运行时所申请的内存。
~point()
{
}
//函数的重载(overload)
// 函数重载的条件:参数类型,参数个数不同
// 函数的重载只能发生在一个类中。
point(int a,int b)
{
x = a;
y = b;
}
void OutPut()
{
cout << x <<endl << y <<endl;
}
//this指针式一个隐藏的指针,它是指向对象本身的,代表了对象的地址
void InPut(int x,int y)
{
this->x = x;
this->y = y;
}
};
void main()
{
point pt(2,3);
//pt.x = 4;
//pt.y = 5;
pt.InPut(3,2);
pt.OutPut();
}
Part 3
#include <iostream.h>
void change(int &a,int &b);
class animal
{
public:
animal(int heigh,int weight)
{
cout << "animal construct" <<endl;
}
~animal()
{
cout << "animal deconstruct" <<endl;
}
void eat()
{
cout << "animal eat" <<endl;
}
void sleep()
{
cout << "animal sleep" <<endl;
}
//虚函数 迟绑定 在基类的函数前面加上virtual时,在派生类中重写该函数
// 运行时会根据对象的实际情况来调用相应的函数,如果对象类型是派生类,
//就调用派生类的函数,如果是基类 就调用基类的函数
virtual void breathe()
{
cout << "animal breathe" <<endl;
}
//virtual void breathe() = 0;
// 纯虚函数 ,含有纯虚函数的类称为抽象类,抽象类不能用来声明对象
};
// 派生类是如何继承基类的
class fish : public animal
{
public:
// 注意当基类中的构造函数是含有参数的时候,如何定义
//派生类中的构造函数
fish() : animal(200,200)
{
cout << "fish construct" <<endl;
}
~fish()
{
cout << "fish deconstruct" <<endl;
}
// 函数的覆盖
// 1 基类的函数必须是虚函数,
// 2 发生覆盖的两个函数要分别位于派生类和基类中
// 3 函数的名称和参数列表必须完全相同
void breathe()
{
cout << "fish bubble" <<endl;
}
// 函数的隐藏
// 1 派生类的函数与基类的函数完全相同(函数名 函数列表)基类函数没有virtual;
// 2 派生类的函数与基类的函数同名,但参数列表不同
// Notice 注意与重载的不同:重载是在一个类中,隐藏是在不同的类中。
void sleep(int m)
{
cout << "fish sleep" <<endl;
}
};
// c++的多态性是由虚函数来实现的,而不是纯虚函数。
void fn(animal* pAn)
{
pAn->breathe();
}
// 引用只是一个别名,是一个变量或对象的名称
// 应用一旦初始化,就代表一块特定的内存
// 应用一般应用在形参的定义上。
void change(int &a,int &b)
{
a = a+b;
b = a-b;
a = a-b;
}
void main()
{
int x = 5;
int y = 3;
animal* an;
fish fh;
// 对于继承类来说,当它声明对象的时候,先调用基类的构造函数,在调用派生类的构造函数
// 当调用结束的时候,先调用派生类的析构函数,再调用基类的析构函数。
an = &fh;
fn(an);
an->breathe();
fh.breathe();
fh.sleep(3);
an->sleep();
an ->eat();
cout << "the original x = "<< x <<endl;
cout << "the original y = "<< y <<endl;
change(x,y);
cout << "the changed x = " << x <<endl;
cout << "the changed y = " << y <<endl;
}
Part 4
基类Animal的头文件
#ifndef ANIMAL_H_H
#define ANIMAL_H_H
// 这个地方选择宏名,主要看ANIMAL_H_H是不是已经定义过了
// 以此来避免类的重复定义,在选择时尽量选择一些不常见的名字
//流程:1 检查ANIMAL_H_H是不是已经定义,如果没有定义就定义ANIMAL_H_H
// 然后顺序执行下去
// 2 如果ANIMAL_H_H已经定义,就跳转到#endif,执行结束
class animal
{
public:
animal();
~animal();
void eat();
void sleep();
virtual void breathe();
protected:
private:
};
#endif
基类Animal的源文件
#include "Animal.h"
#include <iostream.h>
// " "和< >的区别,主要是编译器在搜索头文件时的顺序不同
//< >表示从系统的目录下开始搜索,然后再搜索PATH环境变量所列的目录,不搜索当前目录
//" "表示先从当前目录搜索,然后是系统目录和PATH化境变量所列出的目录 搜索速度相对较慢
animal :: animal()
{
}
// :: 作用域标识符,用于指明一个函数属于那个类或者一个成员变量属于那个类
// :: 前面没有类名,表示是全局函数或全局成员
animal :: ~animal()
{
}
void animal :: eat()
{
cout << "animal eat" <<endl;
}
void animal :: sleep()
{
cout << "animal sleep" <<endl;
}
//如果在头文件中加入virtual后,在源文件中就不必再加virtual
void animal :: breathe()
{
cout << "animal breathe" <<endl;
}
派生类fish的头文件
#ifndef FISH_H_H
#define FISH_H_H
// 子类必须包含基类的头文件
#include "Animal.h"
class fish : public animal
{
public:
void breathe();
};
#endif
派生类fish的源文件
#include "Fish.h"
#include <iostream.h>
void fish ::breathe()
{
cout << "fish bubble" <<endl;
}
主函数
#include "Animal.h"
#include "Fish.h"
#include <windows.h>
// 在设计一个类时,通常将类的定义以及类成员函数的声明放在头文件中
// 将类中成员函数的实现放在源文件中
void fn(animal* pAm)
{
pAm->breathe();
}
void main()
{
animal* pAn;
fish fh;
pAn = &fh;
fn(pAn);
system("pause");
}
//生成exe文件的过程
// 1 编译过程 (compiling)c++编译器对工程中的源代码单独进行编译。
// 在编译时,先由预处理器对预处理指令进行处理,在内存中输出翻译单元(一种临时文件)
// 编译器接受预处理的输出,将源代码转换成包含机器语言指令的目标文件(.obj)在编译时
// 头文件是不参与的
// 2 衔接过程(Linking) 衔接器将目标文件和所用到的C++类库文件一起衔接生成.exe文件。