**本篇博文参考视频见我上一篇博文的第一行**
### 类和对象的声明
- 类的声明
```
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. DAY 5模拟赛

    DAY 5 廖俊豪神仙出题 T1 最小差异矩阵(a.cpp, a.in, a.out) [题目描述] 有一个 n*m 的矩阵,矩阵的每个位置上可以放置一个数.对于第 i 行,第 i 行的差异定义为该行 ...

  2. DeepFaceLab参数详解之FPS与Image format

    DeepFaceLab参数详解之FPS与Image format , 本文讲解两个非常简单的参数,也是大家玩这个软件最先接触到的.参数虽然非常的简单,但是非常有用,适当调整可以为你节省大量硬盘空间,大 ...

  3. 下载的管理类MyDownloadManager

    import android.content.Intent; import android.net.Uri; import java.io.File; import java.io.FileOutpu ...

  4. HttpRunnerManager(二)--使用

    参考资料:https://sutune.me/2018/08/05/httprunner/

  5. Java学习之==>面向对象编程 Part1

    一.面向对象与面向过程 1.面向过程 角度是功能,以方法为最小单位,思考的是具体怎么做. 2.面向对象 角度是抽象,以类为最小单位,思考的是谁来做. 3.示例:“小明去上班” 面向过程 起床,刷牙洗脸 ...

  6. 滚动翻页vue

    <template> <div class="home"> <div style="height:100%; width:100%;&quo ...

  7. [Vuejs] 在vue各个组件中应用全局scss变量

    需要安装一个插件:sass-resources-loader 1.执行安装命令: npm i sass-resources-loader --save-dev 2.修改vue-cli环境下build文 ...

  8. lambda得用法

  9. Spring是如何使用责任链模式的?

    关于责任链模式,其有两种形式,一种是通过外部调用的方式对链的各个节点调用进行控制,从而进行链的各个节点之间的切换. 另一种是链的每个节点自由控制是否继续往下传递链的进度,这种比较典型的使用方式就是Ne ...

  10. mybatis+mysql 返回主键

    需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值. 方法:在mapper中指定keyProperty属性,示例如下: <insert id="i ...