**本篇博文参考视频见我上一篇博文的第一行**
### 类和对象的声明
- 类的声明
```
class People{
int a;
void fun(){
cout<<"fun"<<endl;
}
};
```
**注意:类结束一定要加分号,否则会报void不能有返回值的错误**

- 对象的声明
```
People p;
```

### 访问修饰符(public private proctected friend)
没有访问修饰符,默认是private
public: 类内成员对外可见
private:类内成员对外不可见
protected:类和子类可见
friend:友元,对某函数私有变量访问开放

### 构造函数、构造函数类型
和Java大致相同,这里不过多叙述

### 初始化和赋值的区别
初始化是产生对象后产生自带属性,空参构造中传值
赋值是一个变量可以进行赋予一个值

### 初始化列表
数据成员进行初始化
```
class stu{
public:
int a;
float f;
stu():a(12),f(12.3f){};
};
```
### 析构函数
~类名(){
}

### this指针
this->成员
和Java也差不多

### 常函数
xxx() const{}
可以使用数据成员,不能修改数据成员

### 静态成员
- 静态数据成员
static int a;
- 静态函数成员
static void xx(){}

**调用**
- 类名::成员
- 类名::函数()

### 宏计算
```
#define SUM(x) x*x
cout<<(SUM(2+3))
```
宏计算知识单纯的替换,也就是替换成2+3*2+3=11,输出11

### 运算符重载

```
#include <iostream>
using namespace std;

class stu{
private:
int age;
int score;
public:
stu(){
age=12;
score=12;
}
stu(int age,int score){
this->age=age;
this->score=score;
}

friend stu operator+(stu& s,stu& s2);
friend int operator>=(stu& st1,stu& st2);
friend ostream& operator << (ostream& os,const stu& stu);
friend istream& operator >> (istream& os,stu& stu);
friend int operator++(stu& stu);
friend int operator++(stu& stu,int n);
int operator=(int a){
age=a;
return a;
}
int operator[](int n){
return 0;
}
//强制类型转换
operator int(){
return age;
}
};
stu operator+(stu& s,stu& s2){
stu ss(s.age+s2.age,s.score+s2.score);
return ss;
}
int operator>=(stu& st1,stu& st2){
return st1.age-st2.age;
}
ostream& operator << (ostream& os,const stu& stu){
os<<stu.age;
return os;
}
istream& operator >> (istream& is,stu& stu){
is>>stu.age>>stu.score;
return is;
}

int operator++(stu& stu){
return ++stu.age;
}

int operator++(stu& stu,int n){
return stu.age++;
}

int main(){
stu st1,st2;
st1=st1+st2;
cout<<(st1>=st2)<<endl;
cout<<st1<<endl;
cout<<st1++<<endl;
cout<<++st1<<endl;
cout<<((int)st1)<<endl;
system("pause");
return 0;
}
```
重载几个操作符的写法

### 类的继承
````
#include <iostream>
using namespace std;
class people{
public:
void study(){
cout<<"study"<<endl;
}
};
class child:public people{
public:
void gotoschool(){
cout<<"gotoschool"<<endl;
}
};
class manwoman:public people{
public:
void gotowork(){
cout<<"gotowork"<<endl;
}
};
class old:public people{
public:
void gotorelax(){
cout<<"gotorelax"<<endl;
}
};
int main(){
child ch;
ch.study();
system("pause");
}
```
简单继承

```
#include <iostream>
using namespace std;
class people{
private:
void fun1(){
cout<<"father fun1()"<<endl;
}
protected:
void fun2(){
cout<<"father fun2()"<<endl;
}
public:
void fun3(){
cout<<"father fun3()"<<endl;
}
};
class child:public people{
public:
void fun3(){
fun2();
cout<<"child fun3()"<<endl;
}
};
int main(){
child ch;
ch.fun3();
system("pause");
}
```
权限关系
```
#include <iostream>
using namespace std;
class people{
public:
people(){
cout<<"people"<<endl;
}
};
class child:public people{
public:
child(){
cout<<"child"<<endl;
}
};
int main(){
child ch;
system("pause");
}
```
输出people child,所以先调用父类构造,然后调用子类构造
析构函数,先释放子类,然后释放父类

### 多态
```
#include <iostream>
using namespace std;
class father{
public:
virtual void show(){
cout<<"father"<<endl;
}
};
class son:public father{
public:
int aa;
void show(){
cout<<"son"<<endl;
}
};
int main(){
//父类引用指向子类对象
father* f=new son;
f->show();
son* s=(son*)f;
s->show();
system("pause");
return 0;
}
```
### 虚函数的注意点
父类是虚函数,子类函数自动是虚函数

### 虚析构
释放父类,也同时释放子类

### 纯虚函数
```
virtual void show()=0;
```
类似Java的抽象方法

### 虚继承
```
#include <iostream>
using namespace std;
class A{
public:
int a;
};
class B:virtual public A{
public:
};
class C:virtual public A{
public:
};
class D:public B,public C{

};
int main(){
D d;
d.a;
system("pause");
return 0;
}
```

### 异常
```
#include <iostream>
using namespace std;
void fun(int a){
if(a==0) abort();
}
int main(){
fun(1);
system("pause");
return 0;
}
```
异常终止

```
#include <iostream>
using namespace std;

void fun(int i){
while(i<10){
i++;
if(5==i){
throw i;
}
}
}
int main(){
try{
fun(3);
}catch(int a){
cout<<a<<endl;
}
system("pause");
return 0;
}
```
catch表示throw的类型

### 内部类
```
#include <iostream>
using namespace std;

class out{
public:
int a;
out(){
a=12;
}
public:
class ci{
public:
int b;
ci(){
b=13;
}
void funin(){
out ou;
cout<<ou.a<<endl;
}
};
};
```
内部类示例

### static_cast
```
#include <iostream>
using namespace std;

class father{
public:
int a;
};
class son:public father{
public:
int b;
};
int main(){
father* f;
son* s;
f=(father*)s;//旧类型转换
f=static_cast<father*>(s);//新的类型转换
system("pause");
return 0;
}
```
### const_cast
```
int main(){
const father* f;
son* s;
father* p1=const_cast<father*>(f);
system("pause");
return 0;
}
```
### 函数模板
```
#include <iostream>
using namespace std;
template<typename T>
void fun(T a){
cout<<a<<endl;
}
int main(){
fun("post");
fun(12);
fun(13.5);
system("pause");
return 0;
}
```
template&lg;typenane T>
如果传进参数T,无论传入什么参数,都可以得出函数结果

**函数模板2个参数**
```
#include <iostream>
using namespace std;
template<typename T,typename Y>
void fun(T a,Y b){
cout<<a<<endl;
cout<<b<<endl;
}
int main(){
fun("post",5);
fun(12,5);
fun(13.5,"success");
system("pause");
return 0;
}
```
2个参数可以传进不同参数

### 函数模板的具现化

C++ 6小时刷完面向对象的更多相关文章

  1. 企业面试之LeetCode刷题心得

    谈起刷LeetCode的心得,想要先扯点别的,说实话我是比较自虐的人,大学时候本专业从来不好好上,一直觊觎着别人的专业,因为自己文科生,总觉得没有项技术在身出门找工作都没有底气,然后看什么炫学什么,简 ...

  2. 【Java学习系列】第2课--Java语法及面向对象

    本文地址 分享提纲: 1. Java程序特点 1.1 基本语法 1.2 字符串 1.3 变量 1.4 Java数组 1.5 Java枚举 1.6 Java修饰符 1.7 Java编译制定在制定目录 2 ...

  3. [Java入门笔记] 面向对象编程基础(一):类和对象

    什么是面向对象编程? 我们先来看看几个概念: 面向过程程序设计 面向过程,是根据事情发展的步骤,按进行的顺序过程划分,面向过程其实是最为实际的一种思考方式,可以说面向过程是一种基础的方法,它考虑的是实 ...

  4. 20145208 实验三 Java面向对象程序设计

    20145208 实验三 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...

  5. 20135214万子惠 (2)——-Java面向对象程序设计

    实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计模式 (一)单元测试 (1) 三种 ...

  6. javascript面向对象分层思维

    js本身不是面向对象语言,在我们实际开发中其实很少用到面向对象思想,以前一直以为当要复用的时候才封装成对象,然而随着现在做的项目都后期测试阶段发现面向对象的作用不仅仅只是复用,可能你们会说面向对象还有 ...

  7. 【JavaScript】使用面向对象的技术创建高级 Web 应用程序

    本文讨论: JavaScript 是基于原型的语言 用 JavaScript 进行面向对象的编程 JavaScript 编码技巧 JavaScript 的未来 本文使用了以下技术: JavaScrip ...

  8. iOS 开发-- Runtime 1小时入门教程

    1小时让你知道什么是Objective-C Runtime,并对它有一定的基本了解,可以在开发过程中运用自如. 三.Objective-C Runtime到底是什么东西? 简而言之,Objective ...

  9. javascript面向对象创建高级 Web 应用程序

       目录 JavaScript 对象是词典 JavaScript 函数是最棒的 构造函数而不是类 原型 静态属性和方法 闭包 模拟私有属性 从类继承 模拟命名空间 应当这样编写 JavaScript ...

随机推荐

  1. 浏览器端-W3School-HTML:HTML DOM Area 对象

    ylbtech-浏览器端-W3School-HTML:HTML DOM Area 对象 1.返回顶部 1. HTML DOM Area 对象 Area 对象 Area 对象代表图像映射的一个区域(图像 ...

  2. Windows 10下怎么远程连接 Ubuntu 16.0.4(方案二)

    使用TeamViewer实现远程桌面连接 背景: 有些朋友反映,借助Ubuntu自带的桌面共享工具desktop sharing会有不再同一网端下出现连接不稳定或者掉线的问题,那么现在我们就可以借助第 ...

  3. 创建的项目如果没有src/main/resources文件夹,如何解决?

    这是刚创建的一个maven项目,由此可以看见,项目并没有存放配置文件的src/main/resources文件夹? 解决方案: (1)选中项目,右键单击,如图所示选择:Build Path --> ...

  4. Linq之旅:Linq入门详解(Linq to Objects)(转)

    http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html 示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细 ...

  5. H5、原生app、混合开发三者比较

    一.概念 a) H5:即Html5,接触过互联网的都知道html,所以很明显h5是html的第5次重大修改的一项超文本标记语言的标准协议. b) 原生:使用原生制作APP(Native app),即在 ...

  6. Win10删除右键菜单快捷键方法

    打开注册表,找到HKEY_CLASSES_ROOT\Directory\Background路径,删除对应的快捷方式即可

  7. pycharm中ctrl + C复制, ctrl+A全选等快捷键失效

    原因是:在安装pycharm的时候也同时安装了vim插件,需要在settings  - > vim Emulation里将相关的handler改成 IDE

  8. java 内容仓储

    什么是JAVA内容仓库(Java Content Repository)(2) 转载 自:http://blog.csdn.net/vltic/article/details/7249395 内容仓库 ...

  9. mybatis使用的一点小结:session运行模式及批量提交(转)

    mybatis的执行器有三种类型: ExecutorType.SIMPLE 这个类型不做特殊的事情,它只为每个语句创建一个PreparedStatement. ExecutorType.REUSE 这 ...

  10. 多个电脑上免密登陆命令、scp远程拷贝、修改文件的用户和组

    多个电脑上免密登陆命令: 1.ssh-keygen    生成密钥 2.ssh-copy-id   IP 拷贝公钥到指定服务器并授权 3.ssh   Ip 验证登录,已无需输入密码 scp远程拷贝: ...