**本篇博文参考视频见我上一篇博文的第一行**
### 类和对象的声明
- 类的声明
```
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. fiddler配置会话框菜单栏

    1.添加会话框菜单:鼠标移至会话框菜单的左上角“#”位置,右键>Customize column,Collection下拉菜单选择"Miscellaneous",Field ...

  2. Python实现打印螺旋矩阵功能的方法

    Python实现打印螺旋矩阵功能的方法 本文实例讲述了Python实现打印螺旋矩阵功能的方法.分享给大家供大家参考,具体如下: 一.问题描述 输入N, 打印 N*N 螺旋矩阵 比如 N = 3,打印: ...

  3. VS2013配置curl

    http://blog.csdn.net/totodum/article/details/51059380 安装完成之后,要注意url的传值, curl中需要传char*

  4. c++嵌入linux指令以查找文件夹

    char buf[256]={0}; char cmd[64] ={0}; FILE *fp=NULL; snprintf(cmd,sizeof(cmd),"ls %s",&quo ...

  5. 从零开始学习GDI+ (二) 基本概念与基本操作

    从零开始学习GDI+ (一)我的第一个GDI+程序 上文给新手学习GDI+讲述了vs环境等的准备工作,并且可以直接用GDI+绘图了.本文开始,讲述的可能偏理论,建议学习的过程中大胆尝试,多使用API. ...

  6. ArrayList和LinkedList的底层代码实现思想

    ArrayList是Java众多集合类中的一个,实现List接口,List的父接口是Collection.ArrayList底层的数据结构是线性表中的顺序表,底层是一个长度可以动态增长的数组.数组有很 ...

  7. Hive-Container killed by YARN for exceeding memory limits. 9.2 GB of 9 GB physical memory used. Consider boosting spark.yarn.executor.memoryOverhead.

    Caused by: org.apache.spark.SparkException: Job aborted due to stage failure: Task times, most recen ...

  8. spring boot-4.配置文件

    官方文档的23.4章节介绍了关于配置文件的内容 springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的 ...

  9. __strong修饰符

    本文用来观察,对于__strong修饰符,编译器为我们自动添加了什么代码,这些代码对于引用计数有什么影响. 例子一 X __strong *x1 = [[X alloc] init]; 使用控制台打印 ...

  10. Who will be punished

    Who will be punished Problem Description This time,suddenly,teacher Li wants to find out who have mi ...