概念:
(一)类:类是一种将抽象转换用户定义类型的C++工具,它将数据表示和操纵数据的方法结合为一个整洁的包;
(二)头文件声明
#ifndef Object_hpp
#define Object_hpp
#include <stdio.h>
#include <iostream>
using namespace std;
class Jason{
//私有属性,可以不用声明,默认
private:
double pri_f;
void priTest();
//公有属性
public:
Jason()
{
}
Jason(int i,int b)
{
pub_i = i;
pub_b = b;
}
int pub_i;
int pub_b;
void pubTest();
//this指针
const Jason checkTop(const Jason & j) const;
//加法运算符
Jason operator+(const Jason &t);
Jason operator-(const Jason &t);
Jason operator*(const Jason &t);
Jason operator*(const double t);
Jason operator/(const Jason &t);
//函数重载,类判断时会进入
operator bool();
//半公开属性,除了在类中访问,外部只能是子类访问
protected:
};
//内联函数,如果希望在头文件中实现,加inline
inline void Jason::priTest()
{
cout << "df" << endl;
}
(三)cpp描述文件
#include "Object.hpp"
#pragma mark -
void Jason::pubTest()
{
cout << "pubTest" << endl; priTest();
}
const Jason Jason::checkTop(const Jason &j) const
{
if (j.pub_i > pub_i)
{
return j;
}
return *this;
}
Jason Jason::operator+(const Jason &t)
{
this->pub_i += t.pub_i;
this->pub_b += t.pub_b;
return *this;
}
Jason Jason::operator-(const Jason &t)
{
this->pub_i -= t.pub_i;
this->pub_b -= t.pub_b;
return *this;
}
Jason Jason::operator*(const double t)
{
this->pub_i *= t;
this->pub_b *= t;
return *this;
}
Jason Jason::operator*(const Jason &t)
{
this->pub_i *= t.pub_i;
this->pub_b *= t.pub_b;
return *this;
}
Jason::operator bool()
{
return true;
}
#pragma mark -
void Stock::priTest2()
{
Stock::pri_f = 10;
cout << "priTest2" << endl;
}
#pragma mark -
SoWhat::SoWhat()
{
}
SoWhat::SoWhat(double d,int i,char c)
{
}
SoWhat::~SoWhat()
{
}
(四)构造函数、析构函数
class Stock {
double pri_f;
void priTest2();
public:
//构造函数(无参),系统默认
Stock()
{
}
//构造函数(有参),如果定义了有参构造函数,则系统不会自动生成无参构造函数
Stock(char *input,int i)
{
s_c = new char[10];
strcpy(s_c, input);
}
//成员函数
Stock(int t):ii(t),bb(ii+5)
{
}
Stock(int t,float f):ii(t),bb(ii+5)
{
}
//析构函数,内部还是不要手动去处理好,太纠结的内存处理
~Stock()
{
//delete [] s_c;
}
char *s_c;
int ii;
int bb;
};
//在cpp中实现析构函数
class SoWhat {
public:
SoWhat();
SoWhat(double d,int i, char c);
~SoWhat();
protected:
};
(五)调用
#include <iostream>
#include "Object.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
Jason j;
j.pubTest();
j = j+j;
char c[] = "dfdf";
//初始化方法等价
Stock s;
s = {c,33};
Stock ss(c,10);
Stock sss = {c,14};
Stock s4 = Stock(c, 3);
Stock *s5 = new Stock{c,333};
//两个结构一样的类赋值
s4 = ss;
s5 = &s4;
//对象数组
Stock aryS[3] = {
Stock{c,1},
Stock{c,2},
Stock{c,3}
};
Stock aryA = aryS[0];
//operator运算符重载用法
//结论,右值第一和第二个的值会改变,第一个的值是总值,第二个是二和三的总值,后面的值是参数不变
Jason j1(5,4);
Jason j2(10,12);
Jason j3(344,134);
Jason j4(4,13);
Jason total(11,15);
total = total+j2;
//如果多个类相加的解决办法,
total = total.operator+(j2); //两个相加
total = j1.operator+(j2+j3);//三个相加
total = j1.operator+(j2+j3+j4);//四个相加
total = j1.operator*(j2*j3);
total = j1.operator*(20.0f);
//bool判断
if (total)
{
cout << "s";
}
return 0;
}
(六)this指针
//this指针
const Jason checkTop(const Jason & j) const;
const Jason Jason::checkTop(const Jason &j) const
{
if (j.pub_i > pub_i)
{
return j;
}
return *this;
}
(七)函数后面有 const
例如: void check() const {cout << "hello" << cout << endl ;};