**本篇博文参考视频见我上一篇博文的第一行**
### 类和对象的声明
- 类的声明
```
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. vue组件化初体验 全局组件和局部组件

    vue组件化初体验 全局组件和局部组件 vue组件化 全局组件 局部组件  关于vue入门案例请参阅 https://www.cnblogs.com/singledogpro/p/11938222.h ...

  2. windows运维常用命令

    devmgmt.msc  设备管理器msconfig     启动项命令mstsc        远程登录diskmgmt.msc 磁盘管理  calc.exe     计算器shutdown -r ...

  3. leetcode 160相交链表

    暴力解法当然可以遍历两个链表,不过time O(mn) space O(1)暂且不说, 方法一:双指针, time O(m+n),space O(1) 可以对比判断环形链表的快慢指针法. 这种方法构思 ...

  4. python学习之模块-模块(二)

    5.2 序列化模块 ​ 将一种数据结构转换成一种特殊的序列(字符串或bytes)的过程就叫序列化.这个特殊的序列还可以通过命令反解回原来的数据类型. python中有三种序列化的功能模块: json模 ...

  5. LeetCode.970-强大的整数(Powerful Integers)

    这是悦乐书的第367次更新,第395篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第229题(顺位题号是970).给定两个正整数x和y,如果对于某些整数i >= 0 ...

  6. Opencv之LBP特征(算法)

    LBP(Local Binary Pattern),即局部二进制模式,对一个像素点以半径r画一个圈,在圈上取K个点(一般为8),这K个点的值(像素值大于中心点为1,否则为0)组成K位二进制数.此即局部 ...

  7. 【机器学习】Linear least squares, Lasso,ridge regression有何本质区别?

    Linear least squares, Lasso,ridge regression有何本质区别? Linear least squares, Lasso,ridge regression有何本质 ...

  8. Maven从入门到精通(二)

    上一篇我们讲解了Maven项目的基本目录结构,也已经安装了Maven的开发环境,接下来我们要重点讲解一下Maven最核心的灵魂pom.xml文件 POM:Project Object Model 项目 ...

  9. HDU 1263 水果 (STL map)

    水果 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  10. 三校联训 【NOIP模拟】寻找

    题面 “我有个愿望,我希望穿越一切找到你.” 这是个二维平面世界,平面上有n个特殊的果实,我从(0,0)点出发,希望得到尽量多的果实,但是出于某种特殊的原因,我的运动方式只有三种(假设当前我在(x,y ...