码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 6小时刷完面向对象

时间:2019-06-08 00:39:45      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:父类   类型转换   不同   修饰符   继承   抽象   post   names   static   

**本篇博文参考视频见我上一篇博文的第一行**
### 类和对象的声明
- 类的声明
```
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小时刷完面向对象

标签:父类   类型转换   不同   修饰符   继承   抽象   post   names   static   

原文地址:https://www.cnblogs.com/littlepage/p/10989612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!