#include <iostream>
using namespace std;
#include <string>
//const 修饰成员函数
class person
{
public:
void show()const//常函数内不可修改成员属性
{
// m_a = 100;//常函数内不可修改成员属性
m_b = 100;// mutable 特殊变量,可以在常函数修改
}
void func()
{}
int m_a;
mutable int m_b;// mutable 特殊变量,可以在常函数修改
};
void test()
{
const person p;//常对象 中的属性不可修改,除了特殊变量
//p.m_a = 100;//常对象 中的属性不可修改
p.m_b = 100;
p.show();//常对象 只能调用常函数
//p.func();
}
int main()
{
}
#include <iostream>
using namespace std;
#include <string>
//函数友元
class building
{
friend void goodgay(building* building);//生成友元 可以访问private成员
public:
building()//赋初值
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
public:
string m_sittingroom;
private:
string m_bedroom;
};
//全局函数
void goodgay(building *building)
{
cout << building->m_sittingroom << endl;
cout << building->m_bedroom << endl;//不是友元不可访问private
}
void test()
{
building p;
goodgay(&p);
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//类友元
//类友元
class building
{
friend class gay;//类友元
//本类的好友 可以访问本类
public:
building();类外 构造函数
public:
string m_sittingroom;
private:
string m_bedroom;
};
class gay
{
public:
gay();类外 构造函数
void visit();类外函数
building* p;
};
//类外函数
building::building()//构造函数
{
m_bedroom = "bedroom";
m_sittingroom = "sittingroom";
}
gay::gay()
{
p= new building;//new 是在堆区数据 创建指针
//初始化 指向内存 //new 返回指针类型
}
void gay::visit()
{
cout << p->m_sittingroom << endl;
cout << p->m_sittingroom << endl;
}
void test()
{
gay A;
A.visit();
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//成员函数做友元
class building;//先声明 总分分分分。。形式
class goodgay
{
public:
goodgay();
void visit();
void visit2();
building* b;
};
class building
{
friend void goodgay::visit();//goodgay下的visit函数可以访问building的所有成员
public:
building();
string m_sittingroom;
private:
string m_bedroom;
};
building::building()
{
m_bedroom = "bedroom";
m_sittingroom = "sittingroom";
}
goodgay::goodgay()
{
b = new building;
}
void goodgay::visit()
{
cout << b->m_sittingroom << endl;
cout << b->m_bedroom<< endl;
}
void goodgay::visit2()
{
cout << b->m_sittingroom << endl;
//cout << b->m_bedroom << endl;//visit2不是友元 不可访问
}
void test()
{
goodgay p;
p.visit();
}
int main()
{
test();
}
//运算符重载 因为是新的数据类型 所以运算符不能直接应用,需要重载
#include <iostream>
using namespace std;
#include <string>
//运算符重载 内置的数据类型运算符不能改变;
class person
{
public:
//person operator+(person &p)// 成员函数重载 +
//{
// person temp;
// temp.m_a = this->m_a +p.m_a;
// temp.m_b = this->m_b +p.m_b;
// return temp;
//}
int m_a;
int m_b;
};
person operator+(person &p1,person &p2)// 全局函数 重载 +
{
person temp;
temp.m_a = p1.m_a + p2.m_a;
temp.m_b = p1.m_b + p2.m_b;
return temp;
}
person operator+(person& p1, int num)// 全局函数 重载 + ,输入不同再重载
{
person temp;
temp.m_a = p1.m_a + num;
temp.m_b = p1.m_b + num;
return temp;
}
void test()
{
person p1;
p1.m_a = 10;
p1.m_b = 20;
person p2;
p2.m_a = 30;
p2.m_b = 40;
person p3 = p1 + p2;
// 成员函数重载 + 本质是 person p3 = p1.operator+(p2);
// 全局函数重载 + 本质是 person p3 = operator+(p1, p2);
person p4 = p1 + 100;
cout << p3.m_a << endl;
cout << p3.m_b << endl;
cout << p4.m_a << endl;
cout << p4.m_b << endl;
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//左移运算符重载
//左移运算符重载只能用 全局函数
class person
{
public:
int m_a;
int m_b;
};
ostream & operator<<(ostream &cout ,person &p)
//返回值是地址
{
cout << p.m_a << p.m_b << endl;
return cout;
}
void test()
{
person p;
p.m_a = 10;
p.m_b = 20;
cout << p << endl;//运算符重载 因为p是新的数据类型 所以运算符不能直接应用,需要重载
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//递增运算符重载 ++
//前置递增 返回引用
//后置递增 返回值
class myint
{
friend ostream& operator<<(ostream& cout, myint a);
public:
myint()//构造初始化
{
m_num = 0;
}
//前置++ 重载
myint & operator++()//返回引用是为了一直对一个数据做操作
{
m_num++;
return *this;
}
//后置++ 重载
myint operator++(int)//占位参数 用于区分 前置后置++
{//因为局部对象,会执行完就释放掉,所以要返回值
myint temp = *this;
m_num++;
return temp;//局部对象,会执行完就释放掉
}
private:
int m_num;
};
ostream& operator<<(ostream& cout, myint a)
{
cout << a.m_num ;
return cout;
}
void test()
{
myint a;
cout << ++a << endl;
cout << a << endl;
cout << a++ << endl;
cout << a << endl;
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//赋值运算符重载
class person
{
public:
person(int age)
{
m_age = new int(age);///在堆区自己创建数据 需要自己在析构函数手动释放
}
int* m_age;
~person()//浅拷贝的析构函数 就会出现重复释放的问题
{
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
}
void operator=(person &p)//重载 等号
{
//m_age = p.m_age;//浅拷贝
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
m_age = new int(*p.m_age);//深拷贝
}
};
void test()
{
person p(18);
person p1(20);
person p2(30);
p2=p1 = p;
cout << *p.m_age << endl;
cout << *p1.m_age << endl;
cout << *p2.m_age << endl;
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//关系运算符重载
class person
{
public:
person(string name, int age)
{
m_name = name;
m_age = age;
}
bool operator==(person &p)
{
if (m_age == p.m_age && m_age == p.m_age)
{
return true;
}
return false;
}
string m_name;
int m_age;
};
void test()
{
person p1("abc", 18);
person p2("abc", 18);
if (p1 == p2)
{
cout << "p1 = p2" << endl;
}
else
{
cout << "p1 != p2" << endl;
}
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//函数调用运算符重载
class myprint
{
public:
void operator()(string name)
{
cout << name << endl;
}
string m_name;
};
void test()
{
myprint p;
p("abc");//类似函数调用,所以叫仿函数
myprint()("qwer") ;//匿名函数对象
}
ostream& operator<<(ostream& cout, myprint& p)
{
cout << p.m_name << endl;
return cout;
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//继承
class basepage //父类的东西,后面可以直接继承
{
public:
void header()
{
cout << "111111111" << endl;
}
void header1()
{
cout << "111111111" << endl;
}
void header2()
{
cout << "111111111" << endl;
}
};
class java : public basepage// class 子类 :继承方式 父类
{
public:
void con()
{
cout << "java" << endl;
}
};
class python :public basepage
{
public:
void con()
{
cout << "python" << endl;
}
};
class cpp :public basepage
{
public:
void con()
{
cout << "c++" << endl;
}
};
void test()
{
cout << "java页面" << endl;
java a;
a.header();
a.header1();
a.header2();
a.con();
cout << "-------------" << endl;
cout << "python页面" << endl;
python a1;
a1.header();
a1.header1();
a1.header2();
a1.con();
cout << "-------------" << endl;
cout << "c++页面" << endl;
cpp a11;
a11.header();
a11.header1();
a11.header2();
a11.con();
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//继承方式
class base///父类
{
public:
int m_a;
protected:
int m_b;
private:
int m_c;
};
class son1:public base//子类 公共继承
{/ 公共 和 保护 可访问
public:
void func()
{
m_a = 10;// 公共还是公共
m_b = 20;// 保护还是保护
//m_c = 30;//私有 不可访问
}
};
class son2:protected base //保护继承
{
public:
void func()
{
m_a = 10;// 公共变保护
m_b = 20;// 保护还是保护
//m_c = 30;//私有 不可访问
}
};
class son3 :private base //私有继承
{
public:
void func()
{
m_a = 10;// 公共变私有
m_b = 20;// 保护变私有
//m_c = 30;//私有 不可访问
}
};
void test()
{
son1 s1;
s1.m_a = 10; /// 公共还是公共 保护还是保护
son2 s2;
//s2. /// 公共变保护 保护还是保护
son3 s3;
//s3. /// 公共变私有 保护变私有
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//继承中的对象模型
class base///父类
{
public:
int m_a;
protected:
int m_b;
private:
int m_c;
};
class son :public base//父类所有的属性都会被继承下去 只不过会被隐藏,访问不到
{
public:
int d;
};
void test()
{
son s;
cout << sizeof(s) << endl;
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//继承中同名的成员 函数处理
//调用自己的 s.xxx
//调用父类的 s.父类::xxx
class base
{
public:
int m_A;
base()
{
m_A = 100;
}
void func()
{
cout << "base func" << endl;
}
};
class son :public base
{
public:
int m_A;
son()
{
m_A = 200;
}
void func()//子类中要是有和父类同名的成员函数 就会把父类所有同名的函数隐藏
{
cout << "son func" << endl;
}
};
void test()
{
son s;
cout << s.m_A << endl;//自己的数据
cout << s.base::m_A << endl;//继承的数据
s.base::func();
s.func();
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//同命的静态成员处理
class base
{
public:
static int m_a;//静态成员属性
//类内声明 类外初始化
static void func()
{
cout << "base func" << endl;
}
};
int base::m_a = 100;//类内声明 类外初始化
class son: public base
{
public:
static int m_a;
static void func()
{
cout << "son func" << endl;
}
};
int son::m_a=200;
void test()
{
//通过对象访问
son s;
cout << s.m_a << endl;
cout << s.base::m_a << endl;
s.func();
s.base::func();
//通过类名访问
cout << son::m_a << endl;
cout << son::base::m_a << endl;//第一个::代表通过类名方式访问
//第二个::代表base作用域下的m_a
son::func();
son::base::func();
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//多继承语法
class base1
{
public:
int m_a;
base1()
{
m_a = 100;
}
};
class base2
{
public:
int m_a;
base2()
{
m_a = 200;
}
};
class son :public base1, public base2
//多继承 子类:继承方式 父类1 , 继承方式 父类2
{
public:
son()
{
m_a = 10000;
m_d = 20000;
}
int m_a;
int m_d;
};
void test()
{
son s;
cout << s.m_a << endl;
cout << s.base1::m_a << endl;//父类出现同名 要加作用域
cout << s.base2::m_a << endl;
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//菱形继承 左右两个继承同样的父类,会造成资源浪费
class animal
{
public:
int m_age;
};
class sheep :virtual public animal {};//虚继承 只继承一份数据
class tuo :virtual public animal {};
class sheeptuo :public sheep, public tuo {};
void test()
{
sheeptuo st;
st.m_age = 100;
cout << st.m_age << endl;
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//多态
class animal
{
public:
virtual void speak()//虚函数
//没有 virtual 就是地址早绑定;编译阶段已经绑定
//如果想调用子类各自的函数,那么地址要晚绑定
{
cout << "动物说话" << endl;
}
};
class cat:public animal
{
public:
void speak()//重写父类的函数
//重写 函数返回值类型 函数名 完全相同 参数列表 完全相同
//重载 函数返回值类型 函数名 完全相同 参数列表 不相同
{
cout << "猫叫" << endl;
}
};
void dospeak(animal& a)
{
a.speak();
}
void test()
{
cat a;
dospeak(a);//多个子类继承一个父类,并且子类重写函数,!!函数参数直接写父类即可!!
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//计算器 普通
class cal
{
public:
int m_num1;
int m_num2;
int getresult(string oper)
{
if (oper == "+")
{
return m_num1 + m_num2;
}
else if (oper == "-")
{
return m_num1 - m_num2;
}
else if (oper == "*")
{
return m_num1 * m_num2;
}
}
};
void test()
{
cal a;
a.m_num1 = 10;
a.m_num2 = 20;
cout << a.m_num1 << "+" << a.m_num2 <<"=" << a.getresult("+") << endl;
cout << a.m_num1 << "-" << a.m_num2 <<"=" << a.getresult("-") << endl;
cout << a.m_num1 << "*" << a.m_num2 <<"=" << a.getresult("*") << endl;
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//计算器 多态
class abstractcal
{
public:
virtual int getresult()
{
return 0;
}
int m_num1;
int m_num2;
};
class addcal :public abstractcal
{
public:
int getresult()
{
return m_num1+m_num2;
}
};
class subcal :public abstractcal
{
public:
int getresult()
{
return m_num1 - m_num2;
}
};
class mulcal :public abstractcal
{
public:
int getresult()
{
return m_num1 * m_num2;
}
};
void test()
{
abstractcal* abc = new addcal;
abc->m_num1 = 10;
abc->m_num2 = 100;
cout << abc->getresult() << endl;
delete abc;
abc = new subcal;
abc->m_num1 = 2000;
abc->m_num2 = 1000;
cout <<abc->getresult() << endl;
delete abc;
mulcal q;
q.m_num1 = 200;
q.m_num2 = 100;
cout << q.getresult() << endl;
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//纯虚函数 抽象类
//类中 含有纯虚函数 那么这个类就是抽象类
class base
{
public:
virtual void func() = 0;// 纯虚函数 所以base就是抽象类
//抽象类 1.无法实例化对象
// 2.抽象类的子类要重写纯虚函数,否则也是抽象类
};
class son :public base
{
public:
void func()//2.抽象类的子类要重写纯虚函数,否则也是抽象类
{
cout << "son func" << endl;
}
};
void test()
{
son s;
s.func();
//父类的指针或引用 来 指向子类对象
base* q = new son;
q->func();
delete q;
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//制作饮品
class drinking
{
public:
virtual void boil() = 0;
virtual void brew() = 0;
virtual void incup() = 0;
virtual void putsome() = 0;
void make()
{
boil();
brew();
incup();
putsome();
};
};
class coffee :public drinking
{
public:
void boil()
{
cout << "煮水" << endl;
}
void brew()
{
cout << "冲coffee" << endl;
}
void incup()
{
cout << "倒杯中" << endl;
}
void putsome()
{
cout << "加糖" << endl;
}
};
class tea :public drinking
{
public:
void boil()
{
cout << "煮水" << endl;
}
void brew()
{
cout << "冲tea" << endl;
}
void incup()
{
cout << "倒杯中" << endl;
}
void putsome()
{
cout << "什么都不加" << endl;
}
};
void dodrink(drinking * a)
{
a->make();
delete a;
}
void test()
{
dodrink(new coffee);
dodrink(new tea);
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//虚析构 纯虚析构
class animal
{
public:
virtual void speak() = 0;
animal()
{
cout << "动物 构造" << endl;
}
虚析构可以解决 父类指针释放不干净问题
//virtual ~animal()
//{
// cout << "动物 析构" << endl;
//}
//纯虚析构 需要声明和实现
//有纯虚析构 也属于抽象类
virtual ~animal() = 0;
};
animal::~animal()
{
cout << "动物 纯虚析构" << endl;
}
class cat :public animal
{
public:
cat(string name)//构造函数
{
cout << "cat 构造" << endl;
m_name = new string (name);
}
void speak()
{
cout << *m_name<<" miao miao" << endl;
}
~cat()//构造函数
{
if (m_name != NULL)
{
cout << "cat 析构" << endl;
delete m_name;
m_name = NULL;
}
}
string* m_name;
};
void test()
{
animal* p = new cat("tom");
p->speak();
//父类指针析构 不会调用子类析构函数 导致子类有堆区属性出现内存泄露
//构造 和 析构顺序顺序相反
delete p;
}
int main()
{
test();
}
#include <iostream>
using namespace std;
#include <string>
//电脑组装
class cpu
{
public:
virtual void cal() = 0;
};
class card
{
public:
virtual void show() = 0;
};
class mermory
{
public:
virtual void storage() = 0;
};
class computer
{
public:
computer(cpu * cpu,card * card,mermory * mermory)
{
m_cpu = cpu;
m_card = card;
m_mermory = mermory;
}
void dowork()
{
m_cpu->cal();
m_card->show();
m_mermory->storage();
}
~computer()
{
if (m_cpu != NULL )
{
delete m_cpu;
m_cpu = NULL;
}
if(m_card != NULL)
{
delete m_card;
m_card = NULL;
}
if (m_mermory != NULL)
{
delete m_mermory;
m_mermory = NULL;
}
}
cpu * m_cpu;
card* m_card;
mermory* m_mermory;
} ;
class intelcpu:public cpu
{
public:
void cal()
{
cout << "intel cpu work" << endl;
}
};
class intelcard :public card
{
public:
void show()
{
cout << "intel card work" << endl;
}
};
class intelmermory :public mermory
{
public:
void storage()
{
cout << "intel mermory work" << endl;
}
};
class lenovocpu :public cpu
{
public:
void cal()
{
cout << "lenovo cpu work" << endl;
}
};
class lenovocard :public card
{
public:
void show()
{
cout << "lenovo card work" << endl;
}
};
class lenovomermory :public mermory
{
public:
void storage()
{
cout << "lenovo mermory work" << endl;
}
};
void test()
{
cpu* Intelcpu = new intelcpu;
card* Intelcard = new intelcard;
mermory* Intelmermory = new intelmermory;
computer* C = new computer(Intelcpu, Intelcard, Intelmermory);
C->dowork();
computer* C1 = new computer(new lenovocpu,new lenovocard,new lenovomermory);
C1->dowork();
}
int main()
{
test();
}
C++学习路程 22/3/12 3/13
于 2022-03-13 22:41:29 首次发布