c++入门基础

c++初级 案例分享

1.栈区

#include <iostream>
using namespace std;

//栈区数据注意事项 --不要返回局部变量的地址
//栈区的数据由编译器管理开辟和释放
int* func(int b)//形参数据也会放着栈区
{
	b=100;
	int a=10;//局部变量 存放在栈区,栈区的数据在函数执行完后自动释放
	return &a;//返回局部变量的地址
}
int main()
{
	//接受func函数的返回值
	int *p=func(1);
	cout<<*p<<endl;//第一次可以打印正确的数字,是因为编译器做了保留
	cout<<*p<<endl;//第二次打印不了
	system("pause");
	return0;
}

2.堆区

#include <iostream>
using namespace std;
int *func()
{
	//利用new关键字可以将数据开辟到堆区
	//指针 本质也是局部变量,放在栈上,指针保存的数据是放在堆区
	int *p=new int(10);//利用new关键字,去创建一个堆区的数据
	               //数据创建好后,他并不是把本身给你,而是把地址传递给你
	               //所有用指针来接受地址
	return p;
}

int main()
{
	int *p=func();
	cout<<*p<<endl;
	system("pause");
	return 0;
}

new运算符

#include <iostream>
using namespace std;

//1.new的基本语法
int *func()
{
	//在堆区创建整形数据
	//new返回的是 该数据类型的指针
int *p=new int(10);
return p;
}
void test01()
{//先接收
int *p=func();
	cout<<*p<<endl;
	cout<<*p<<endl;
	cout<<*p<<endl;
	//堆区的数据 由程序员管理开辟,程序员管理释放
	//如果想释放堆区的数据,利用关键字 delete
	delete p;
	//cout<<*p<<endl;内存已经被释放,再次访问就是非法操作,会报错
}
//2,在堆区利用new开辟数组
void test02()
{
	//创建10整形数据的数组,在堆区
	int *arr=new int[10];//10代表数组有10个元素
	for(int i=0;i<10;i++)
	{
		arr[i]=i+100;//给10个元素赋值 100~109
	}
	for(int i=0;i<10;i++)
	{
		cout<<arr[i]<<endl;
	}
	//释放堆区数组
	//释放数组时,要加上[]才可以
	delete[] arr;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

类1

#include<iostream>
using namespace std;
class Date
{
	public:
	 void setDate(int y,int m,int d);
	 void showDate();
	 private:
	   int year,month,day;
};
void Date::setDate(int y,int m,int d)
{
	year=y;
	month=m;
	day=d;
}
void Date::showDate()
{
	cout<<month<<"-"<<day<<"-"<<year<<endl;
}
int main()
{
	Date D;
	int year,month,day;
	cout<<"输入日期";
	cin>>year>>month>>day;
	D.setDate(year,month,day);
	D.showDate();
	return 0;
}

c++递归求模

#include <iostream>
using namespace std;
int fn(int n)
{
	int m=1;
	for(int i=1;i<=n;i++)
	{
		m*=i;
	}
	return m;
}
int main()
{
	int n;
	int sum=0;
	cout<<"请输出n的值:";
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		sum+=fn(i);
	}
	cout<<"1!+2!+3!+...n!="<<sum<<endl;
	return 0;
}	

不同类型度数转换

#include <iostream>
using namespace std;
int fn(int a)
{
	int b;
	b=5.0/9*(a-32);
	return b;
}
int main()
{
	int a,b;
	cout<<"请输入度数"<<endl;
	cin >>a;
	b=fn(a);
	cout<<"对应的摄氏温度为"<<b<<endl;
	return 0;
	system("pause");
}
	

类之基本信息

#include <iostream>
#include <string>
using namespace std;
class Student
{
	public:
	void setStudent(int n,char na[],int a,char addr[]);
	void showStudent();
   private:
   int no;
   char name[20];
   int age;
   char address[50];
};
void Student::setStudent(int n,char na[],int a,char addr[])
{
	no=n;
	strcpy(name,na);
	age=a;
	strcpy(address,addr);
}
void Student::showStudent()
{
	cout<<no<<" "<<name<<" "<<age<<" "<<address<<endl;
}
int main()
{
	Student std;
	std.setStudent(1,"Mary",20,"江西省南昌市");
	std.showStudent();
	return 0;
}

关于继承

#include  <iostream>
using namespace std;
//继承中的对象模型
class Base
{
	public:
	     int m_A;
	protected:
	//  l  int m_B;
	private:
	   int m_C;
};
class Son:public Base
{
	public:
	int m_D;
};
//利用开发人员命令提示工具查看对象模型
//跳转盘符 F:
//跳转文件路径cd 具体路径下
//查看命名
//cl /d1 reportSingleClassLayout类的类名 文件名
void test01()
{
	//4 12 16
	//父类中所有的非静态成员都会被子类继承下去
	//父类中私有成员属性 是被编译器给隐藏了,因此是访问不到,但是
	//确实被继承下去了
	cout<<"size of son="<<sizeof(Son)<<endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

新增string类型

#include <iostream>
using namespace std;
//#include <string>
int main()
{
	string str2="hello world";
	cout<<str2<<endl;
	system("pause");
	return 0;
}

水仙花数

#include <iostream>
using namespace std;
int main()
{
	int num=100;
	do
	{
		int a=0;
		int b=0;
		int c=0;
	   a=num%10;
	   b=num/10%10;
	   c=num/100;
	   if(a*a*a+b*b*b+c*c*c==num)
	{
		cout<<num<<endl;
	}
	num++;
	}
	while(num<1000);
	system("pause");
	return 0;
}

类与对象初始化

#include <iostream>
using namespace std;
class Person
{
	public:
	//传统初始化操作
	/*Person(int a,int b,int c)
	{
		m_A=a;
		m_B=b;
		m_C=c;
	}*/
	//初始化列表初始化属性,注意冒号的位置
	Person(int a,int b,int c):m_A(a),m_B(b),m_C(c)
	{
	 
	}
	int m_A;
	int m_B;
	int m_C;
};
void test01()
{
	//Person p(10,20,30);
	Person p(30,20,10);
	cout<<"m_A="<<p.m_A<<endl;
		cout<<"m_B="<<p.m_B<<endl;
			cout<<"m_C="<<p.m_C<<endl;
}
	
int main()
{
	test01();
	
	system("pause");
	return 0;
}

类对象作为类成员

#include <iostream>
using namespace std;
#include<string>
//类对象作为类成员

//手机类
class Phone
{
	public:
	Phone(string name)
	{
		m_PhoneName=name;
		cout<<"phone构造"<<endl;
	}
	~Phone()
	{
		cout<<"Phone析构"<<endl;
	}
	//手机品牌名称
	string m_PhoneName;
};
//人类
class Person
{
	public:
	//phone m_Phone=pName;
	Person(string name,string pName):m_Name(name),m_Phone(pName)
	{
		cout<<"Person构造"<<endl;
	}
	~Person()
	{
		cout<<"Person析构"<<endl;
	}

	void playGame()
	
	{
		cout<<m_Name<<"使用"<<m_Phone.m_PhoneName<<"牌手机"<<endl;
	}
	//姓名
	string m_Name;
	//手机
	Phone m_Phone;
};
void test01()
{
	//当类中成员是其他类对象时,我们称该成员为 对象成员
	//构造的顺序是:先调用对象的构造,再调用本类构造
	//析构顺序与构造相反
	Person p("张三","苹果max");
//	cout<<p.m_Name<<"拿着:"<<p.m_Phone.m_PName<<endl;
p.playGame();
}
int main()
{
  test01();
	system("pause");
	return 0;
}

关于友元

#include <iostream>
//全局函数做友元
using namespace std;
#include<string>
class Building
{
	friend void goodGay(Building *building);
	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;
	
}
void test01()
{
    Building building;
	goodGay(&building);//需取地址符号
}
int main()
{
	test01();
	system("pause");
	return 0;
}

指针与数组

#include <iostream>
using namespace std;
int main()
{
	//指针和数组
	//利用指针访问数组中的元素
	int arr[10]={1,2,3,4,5,6,7,8,9,10};
	cout<<"第一个元素为:"<<arr[0]<<endl;
	int * p=arr;//数字名就是数组的首地址
	cout<<"利用指针访问第一个元素:"<<*p<<endl;
	p++;
	cout<<"利用指针访问第二个元素"<<*p<<endl;
	cout<<"利用指针便历数组"<<endl;
	int *p2=arr;
   cout<<*p2<<endl;
	for(int i=0;i<10;i++)
	{
		cout<<arr[i]<<endl;
	}
	
	system("pause");
	return 0;
}

类易错

#include <iostream>
#include <string>
using namespace std;
class Student
{
	public:
	//void setStudent(int n,int na[],int a,int.add[]).为错误的写法
	void setStudent(int n,char na[],int a,char add[]);
	void showStudent();
	private:
		int no;
		char name[20];
		int age;
		char address[50];
};
void Student::setStudent(int n,char na[],int a,char add[])
{
	no=n;
	//错误n[]=name[];
	strcpy(name,na);
	age=a;
	//add[]=address[];
	strcpy(address,add);
}
void Student::showStudent()
{
	cout<<no<<name<<age<<address<<endl;
}
int main()
{
	Student D;
    D.setStudent(1,"mary",18,"江西省南昌市");
    D.showStudent();
	system("pause");
	return 0;
}

复制构造函数的实现

#include <iostream>
using namespace std;
class Point
{
	public:
	Point(int xx=0,int yy=0){x=xx;y=yy;}
	Point(const Point& p);
	~Point();
	void setX(int xx){x=xx;}
	void setY(int yy){y=yy;}
	int getX(){return x;}
	int getY(){return y;}
	private:
	int x,y;
};
//复制构造函数的实现
Point::Point(const Point& p)
	{
		x=p.x;
		y=p.y;
		cout<<"调用复制构造函数!"<<endl;
	}
	//析构函数的实现
	Point::~Point()
	{
		cout<<"调用析构函数!"<<endl;
	}
	//形参为Point类对象的函数
	void fun1(Point p)
	{
		cout<<p.getX()<<endl;
	}
	//返回值为point类对象的函数
	Point fun2()
	{
		Point a(1,2);
		return a;
	}
	//主程序
int main()
{
	Point A(4,5);
	Point B=A;
	cout<<B.getX()<<endl;
	fun1(B);
	B=fun2();
	cout<<B.getX()<<endl;
	system("pause");
	return 0;
}

结构体

#include <iostream>
using namespace std;
#include <string>
struct Student
{
	//成员列表
	
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
};
//通过学生类型创建具体学生
	int main()
	{
	//2.1
	struct Student s1;
	//给s1属性赋值,通过访问结构体变量中的属性
	s1.name="张三";
	s1.age=20;
	s1.score=100;
	cout<<"姓名"<<s1.name<<"年龄"<<s1.age<<"成绩"<<s1.score<<endl;
	}

拷贝类

#include <iostream>
using namespace std;
class Person
{
	public:
	Person()
	{
		cout<<"Person默认构造函数调用"<<endl;
	}
	Person(int age)
	{
		cout<<"Person有参构造函数调用"<<endl;
		m_Age=age;
	}
	Person(const Person & p)
	{
		cout<<"Person拷贝构造函数调用"<<endl;
		m_Age=p.m_Age;
	}
	~Person()
	{
		cout<<"Person析构函数调用"<<endl;
	}
	int m_Age;
};
void text01()
{
	Person p1(20);
	Person p2(p1);
	cout<<"p2的年龄为"<<p2.m_Age<<endl;
}
void doWork(Person p)
{
	
}
void text02()
{
	Person p;
	doWork(p);
}
//值传递返回局部对象
Person doWork2()
{
	
	Person p1;
	cout<<(int*)&p1<<endl;
	return p1;
}
void text03()
{
	Person p=doWork2();
	cout<<(int*)&p<<endl;
}
int main()
{
	//text01();
	//text02();
text03();
system("pause");
return 0;
}

浅拷贝 深拷贝

#include <iostream>
using namespace std;
class Person
{
	public:
	           Person()
	           {
	           	cout<<"Person的默认构造函数调用"<<endl;
	           }
	           Person(int age,int height)
	           {
	           	m_Age=age;
	           	m_Height=new int(height);
	           	cout<<"Person的有参构造函数调用"<<endl;
	           }
	           //"自己实现拷贝构造函数解决浅拷贝带来的问题"
	           Person(const Person &p)
	           {
	           	cout<<"Person拷贝构造函数调用"<<endl;
	           	m_Age=p.m_Age;
	           	m_Height=p.m_Height;
	           	//m_Height=p.m_Height;
	           	//深拷贝操作
	           	m_Height=new int(*p.m_Height);
	           }
	            ~Person()
	           {
	           	//析构代码,将堆区开辟数据做释放操作
	           	//浅拷贝带来的问题是堆区的内存重复释放
	           	if(m_Height !=NULL)//如果身高不为空,释放它指向堆区内存
	           	{
	           		delete m_Height;
	           		m_Height=NULL;
	           	}
	           	cout<<"Person的析构构造函数调用"<<endl;
	           }
	           
	           int m_Age;//年龄
	           int *m_Height;//身高
};
void text01()
{
	Person p1(18,160);
	cout<<"p1的年龄为"<<p1.m_Age<<"身高为"<<*p1.m_Height<<endl;
	Person p2(p1);
		cout<<"p2的年龄为"<<p2.m_Age<<"身高为"<<*p2.m_Height<<endl;
	
}
int main()
{
	text01();
	system("pause");
}	           

常函数 常变量

#include <iostream>
using namespace std;
//常函数 成员函数后加const,称为常函数
//常函数不可以修改成员属性
//成员属性声明时加关键字mutable后,在常函数中依然可以修改
class Person
{
	public:
	//this 指针的本质 是指针常量 指针的指向是不可修改的
	//const Person*const this;
	//在成员函数后面加const,修饰的是this指向,让指针指向的值也不可以修改
	void showPerson() const
	{
		this->m_B=100;
		//this->m_A=100;
		//this=NULL;//this指针不可以修改指针的指向的
	}
	void func()
	{
	}
	int m_A;
mutable	int m_B;//特殊变量,即使在常函数中,也可以修改这个值。
//加关键字mutable
                
};
void test01()
{
	Person p;
	p.showPerson();
	cout<<p.m_B<<endl;
}
/*
void test02()
{
	const Person p;//在对象前加入const,变为常对象
//	p.m_A=100;
	p.m_B=100;//m_B是特殊值,在常对象下也可以修改
	p.showPerson();
}*/
//常对象
//常对象只能调用常函数
//p.func();常对象不可以调用普通成员函数,因为普通成员函数可以修改属性
int main()
{
	test01();
	
	
	system("pause");
	return 0;
}

指针 通透

#include <iostream>
using namespace std;
int main()
{
	//1.定义指针
	int a=10;
	//指针定义的语法:数据类型*指针变量名
	//指针记录的就是地址
	int * p;
	p=&a;
	cout<<"a的地址为"<<&a<<endl;
	cout<<"指针p为"<<p<<endl;
	//2,使用指针,可以通过解引用的方式来找到指针指向的内存
	//*p  (我们称之为解引用),找到指针指向的内存中的数据
	*p=1000;
	cout<<"a="<<a<<endl;
	cout<<"*p="<<*p<<endl;
	system("pause");
	return 0;
}

指针大小相等

#include <iostream>
using namespace std;
//x64位操作系统
int main()
{
	//指针所占的内存空间
	int a=10;
	int *p;
	p=&a;
	//上面所写的也可以简写为 int *p=&a;
	cout<<"sizeof(int*)="<<sizeof(int *)<<endl;
		cout<<"sizeof(int*)="<<sizeof(float *)<<endl;
			cout<<"sizeof(int*)="<<sizeof(char *)<<endl;
				cout<<"sizeof(int*)="<<sizeof(double*)<<endl;
	
	system("pause");
	return 0;
}

关于空指针

#include <iostream>
using namespace std;
int main()
{
	//空指针用于给指针变量进行初始化
	int *p=NULL;
	//空指针是不可以进行访问的
	//0-255之间的内存编号是系统占用的,因此不可以访问
	cout<<*p;
	
	
	system("pause");
	return 0;
	
}

值传递 地址传递

#include <iostream>
using namespace std;
//实现两个数字进行交换
//形参发生了改变,但是实参并未改变
//值传递,实参未发生改变
void swap01(int a,int b)
{
	int temp=a;
	a=b;
	b=temp;
	cout<<a<<endl;
	cout<<b<<endl;
}
//指针保存的是地址
//地址传递会改变实参
void swap02(int *p1,int *p2)
{
	int temp=*p1;
	*p1=*p2;
	*p2=temp;
}
int main()
{
	//指针和函数
	//1,值传递
	int a=10,b=20;
	//cin>>a>>b;
	//swap01(a,b);
	//地址传递
	//如果是地址传递,可以修饰实参
	swap02(&a,&b);
	cout<<a<<endl;
	cout<<b<<endl;
}

冒泡法

#include <iostream>
using namespace std;
void bubbleSort(int *arr,int len)
{
	for(int i=0;i<len-1;i++)
	{
		for(int j=0;j<len-i-1;j++)
		//如果j>j+1的值,交换数字
		{
			if(arr[j]>arr[j+1])
			{
				int temp=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp;
			}
		}			
}
}
void printArray(int *arr,int len)
{
	for(int i=0;i<len;i++)
	cout<<arr[i]<<endl;
}
int main()
{
	//1,创建数组
	int arr[10]={4,3,6,9,1,2,10,8,7,5};
	//数组长度
	int len=sizeof(arr)/sizeof(arr[0]);
	
	//2,创建函数,实现冒泡排序
	bubbleSort(arr,len);
	//3,打印排序后的数组
	printArray(arr,len);
	cout<<*arr<<endl;
	system("pause");
	return 0;
}

类的访问权限

#include <iostream>
using namespace std;
class Base
{
	public:
	   static int a;
	   protected:
	   static int b;
	   private:
	   static int c;
};
int Base::a=10;
int Base::b=20;
int Base::c=30;
class Derived:public Base
{
	public:
	void show()
	{
		cout<<a<<endl;
		cout<<b<<endl;
	//	cout<<Base::c<<endl;
	}
};
class DDerived:public Derived
{
	public:
	void show()
	{
		cout<<Base::a<<endl;
		cout<<Base::b<<endl;
		//cout<<Base::c<<endl;
	}
};
int main()
{
	Derived d;
	DDerived dd;
	d.show();
	dd.show();
	return 0;
}

还是结构体

#include <iostream>
using namespace std;
#include <string>
struct student
{
	string name;//姓名
	int age;//年龄
	int score;//学分
}stu3;
int main()
{
	//结构体变量创建方式1
	struct student stu1;//struct 关键字可以省略
	stu1.name="张三";
	stu1.age=18;
	stu1.score=100;
	cout<<"年龄"<<stu1.age<<"姓名"<<stu1.name<<"学分"<<stu1.score<<endl;
	//结构体变量创建方式2
	struct student stu2={"李四",19,60};
		cout<<"年龄"<<stu2.age<<"姓名"<<stu2.name<<"学分"<<stu2.score<<endl;
		stu3.name="王五";
		stu3.age=60;
		stu3.score=59;
		cout<<"年龄"<<stu3.age<<"姓名"<<stu3.name<<"学分"<<stu3.score<<endl;
	
}	

结构体数组

#include <iostream>
using namespace std;
#include <string>
//结构体数组
struct Student 
{
	string name;
	int age;
	int score;
};
//创建结构体数组
Student stuArray[3]=
{
	{"张三",18,100},
	{"李四",28,99},
	{"王五",38,66}
};
int main()
{
//3.给结构体中的元素赋值
stuArray[2].name="赵六";
stuArray[2].age=80;
stuArray[2].score=60;
//遍历结构体数组
for(int i=0;i<3;i++)
{
	cout<<"姓名"<<stuArray[i].name
	<<"年龄:"<<stuArray[i].age
	<<"学分"<<stuArray[i].score<<endl;
}
system("pause");
return 0;
}

类做友元

#include <iostream>
using namespace std;
#include<string>
//类做友元
class Building;

class Building
{
	friend class GoodGay;
	public:
	Building();
	public:
	string m_SittingRoom;//客厅
	private:
	string m_BedRoom;
};
class GoodGay
{
	public:
	GoodGay();
	void visit();//参观函数 访问Building中的属性
	Building *building;
};
//类外写成员函数
Building::Building ()
{
	m_SittingRoom="客厅";
	m_BedRoom="卧室";
}
GoodGay::GoodGay()
{
	//创建建筑物对象
	building=new Building;
	//这里的building可以看做p,也就是指针p
}
void GoodGay::visit()
{
	cout<<"好基友类正在访问"<<building->m_SittingRoom<<endl;
	cout<<"好基友类正在访问"<<building->m_BedRoom<<endl;
}
void test01()
{
	GoodGay gg;
	gg.visit();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

运算符重载

#include <iostream>
using namespace std;
//加号运算符重载
class Person
{
	public:
	//1.成员函数重载+号
	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;
}
void test01()
{
	Person p1;
	p1.m_A=10;
	p1.m_B=10;
	Person p2;
	p2.m_A=10;
	p2.m_B=10;
	//成员函数本质调用
//	Person p3=p1.operator +(p2);//此行为下一行的本质
//	Person p3=p1+p2;
	//运算符重载 也可以发生函数重载
	//全局函数的本质调用
	Person p3=operator+(p1,p2);
	cout<<"p3.m_A="<<p3.m_A<<endl;
		cout<<"p3.m_B="<<p3.m_B<<endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

左移运算符重载

#include <iostream>
using namespace std;
//左移运算符
class Person
{
	friend ostream&  operator<<(ostream &cout,Person &p);
	public:
	Person(int a,int b)
	{
		m_A=a;
		m_B=b;
	}
	private:
	//利用成员函数重载 左移运算符p.operator<<(cout)
	//简化版本p<<cout
	//不会利用成员函数重载<<运算符,因为无法实现,cout在左侧
//	void operator<<(Person)
	    int m_A;
	    int m_B;
};
//只能运用全局函数重载左移运算符
ostream&  operator<<(ostream &cout,Person &p)
//本质 operator<<(cout,p) 简化cout<<p
{
	cout<<"m_A"<<p.m_A<<"m_B="<<p.m_B;
	return cout;
}
void test01()
{
	Person p(10,10);
	cout<<p<<"hello word"<<endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

重载递增

#include <iostream>
using namespace std;
//重载递增运算符

//自定义整形
class MyInteger
{
	friend ostream& operator<<(ostream& cout,MyInteger myint);
	public:
	MyInteger()
	{
		m_Num=0;
	}
	//重载前置++运算符 返回引用为了一直对一个数据进行递增操作
	MyInteger& operator++()
	{
		m_Num++;//先进行++运算
		return *this;//再将自身做返回
	}
	//重载后置++运算符
//	void operator++(int)//int 表示占位参数,可以用为区分前置和后置
	//递增
	MyInteger operator++(int)
	{
		//先 记录当时结果
		MyInteger temp=*this;
		//后递增
		m_Num++;
		//最后将记录的结果做返回
		return temp;
	}
		
	private:
	int m_Num;
};
//重载<<运算符
ostream& operator<<(ostream& cout,MyInteger myint)
{
	cout<<myint.m_Num;
	return cout;
}
void test01()
{
   MyInteger myint;
   cout<<++(++myint)<<endl;
}
void test02()
{
	MyInteger myint;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

重载

#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 (this->m_Name==p.m_Name && this->m_Age==p.m_Age)
		{
			return true;
		}
		return false;
	}
	bool operator!=(Person &p)
	{
		if(this->m_Name==p.m_Name&& this->m_Age==p.m_Age)
		{
			return false;
		}
		return true;
	}
	string m_Name;
	int m_Age;
};
void test01()
{
	Person p1("Tom",18);
	Person p2("Tom",18);
	if(p1==p2)
	{
		cout<<"p1与p2是相等的"<<endl;
	}
	else
	cout<<"p1和p2是不相等的"<<endl;
	if(p1!=p2)
	{
		cout<<"p1与p2是不相等的"<<endl;
	}
	else
	cout<<"p1和p2是相等的"<<endl;

}
int main()
{
	test01();
	system("pause");
	return 0;
}

函数调用运算符重载

#include <iostream>
using namespace std;
#include <string>
//函数调用运算符重载

//打印输出类
class MyPrint
{
	public:
	//重载函数调用运算符
	void operator()(string test)
	{
		cout<<test<<endl;
	}
};
void MyPrint02(string test)//函数调用
{
	cout<<test<<endl;
}
void test01()
{
	MyPrint myPrint;
	myPrint("hello world");//由于使用起来非常类似于函数调用,因此称为防函数
	MyPrint02("hello world");
}
//仿函数非常灵活,没有固定的写法
//加法类
class MyAdd
{
	public:
		int operator()(int num1,int num2)
		{
		return num1+num2;
		}
};
void test02()
{
	MyAdd myadd;
	int ret=myadd(100,100);
	cout<<ret<<endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

构造与析构顺序

#include <iostream>
using namespace std;
//继承中的构造和析构顺序
class Base
{
	public:
	Base()
	{
		cout<<"Base构造函数"<<endl;
	}
	~Base()
	{
		cout<<"Base的析构函数"<<endl;
	}
	protected:
	
	private:
	
};
	class Son:public Base
	{
		public:
		Son()
	{
		cout<<"Son构造函数"<<endl;
	}
	~Son()
	{
		cout<<"Son的析构函数"<<endl;
	}
	protected:
	
	private:
	};
	void	test01()
		{
			Base b;
		//	Son a;
		}
			
int main()
{
	test01();
	system("pause");
	return 0;
}

空指针可以调用成员函数

#include<iostream>
using namespace std;
//空指针可以调用成员函数
class Person
{
	public:
	void showClassName()
	{
		cout<<"this is Person class"<<endl;
	}
	void showPersonAge()
	{
		//报错的原因是传入的指针NULL
		if(this==NULL)
		{
			return;
		}
		cout<<"age="<<this->m_Age<<endl;
	}
	int m_Age;
};
void test01()
{
	Person * p=NULL;
	p->showClassName();
    //p->showPersonAge();
}
int main()
{
	test01();
	system("pause");
}

常量指针 指针常量

#include <iostream>
using namespace std;
int main()
{
	//1.const修饰指针 常量指针
	int a=10;
	int b=10;
	const int *p=&a;
	//指针指向的值不可以改,指针的指向可以改
	//*p=20;错误
	//2.const修饰常量
	p=&b;//正确
	//2.const修饰常量 指针常量
	//指针的指向不可以改,指针指向的值可以改
	int *const p2=&a;
	*p2=100;//正确的
	//错误p2=&b;
	//3.const修饰指针和常量
	const int * const p3=&a;
	//指针的指向和指针指向的值 都不可以改变
   //*p3=100;//错误
 //p3=&p;
	
	system("pause");
	return 0;
}

this 指针

#include <iostream>
//this指针是隐含每一个非静态成员函数内的一种指针
//this指针不需要定义,直接使用即可,当形参与成员变量同名时
//可用this指针开区分
using namespace std;
//解决名称冲突
//返回对象本身用*this
class Person
{
	public:
	Person(int age)
	{
		//this指针指向 被调用的函数 所属的对象
		this->age=age;
	}
	//注意
	Person& PersonAddAge(Person &p)
	{
		this->age +=p.age;
		return *this;
	}
	int age;
};
void test01()
{
	Person p1(18);
	cout<<"p1的年龄为"<<p1.age<<endl;
}
//返回对象本生时用*this
void test02()
{
	Person p1(10);
	Person p2(10);
	//链式编程思想
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
	cout<<"p2的年龄为"<<p2.age<<endl;
}
	
int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

逆序排序

#include <iostream>
using namespace std;
int main()
{
	int arr[5]={1,2,6,7,8};
	for(int i=0;i<5;i++)
	cout<<"逆序前的排序为"<<arr[i]<<endl;
	int temp;
	int start=0;
	int end;
	end=sizeof(arr)/sizeof(arr[0])-1;
	while(start<end)
	{
	temp=arr[start];
	arr[start]=arr[end];
	arr[end]=temp;
	start++;
	end--;
	}
	cout<<"逆序后的排列为"<<endl;
	for(int i=0;i<5;i++)
	{
		cout<<arr[i]<<endl;
	}
system("pause");
}

敲桌子小游戏

#include <iostream>
using namespace std;
int main()
{
	for(int i=1;i<=100;i++)
	{cout<<i<<endl;
		if((i%7==0)||(i%10==7)||(i/10==7))
		{
			cout<<"敲桌子"<<endl;
		}
	}
	system("pause");
	return 0;
}

结构体小案例

#include <iostream>
using namespace std;
#include <string>
#include <ctime>
//学生的结构体
struct Student
{
	//姓名
	string sName;
	//分数
	int score;
};
struct Teacher
{
	//姓名
	string tName;
	//学生数组
	struct Student sArray[5];
};
//给老师和学生赋值的函数
void allocateSpace(struct Teacher tArray[],int len)
{
	string nameSeed="ABCDE";
	//给老师开始赋值
	for(int i=0;i<len;i++)
	{
		tArray[i].tName="Teacher_";
		tArray[i].tName+=nameSeed[i];
		//通过循环给每每名老师所带的学生赋值
		for(int j=0;j<5;j++)
		{
			tArray[i].sArray>[j].sName="Student_";
			tArray[i].sArray>[j].sName+=nameSeed[j];
			int random=rand()%61+40;
			tArray.sArray[j].score=random;
		}
		//tArray[i].tName
	}
}
//打印所有信息
void orintInfo(struct Teacher[],int len)
{
	for(int i=0;i<len;i++)
	{
		cout<<"老师的姓名"<<tArray[i].tName<<endl;
		for(int j=0;j<5;j++)
		{
			cout<<"\t学生姓名"<<tArray[i].sArray[j].sName<<
			"考试分数"<<tArray[i].sArray[j].score<<endl;
		}
	}
}
int main()
{
	//随机数种子
	srand((unsigned int)time(NULL));
	//创建3名老师的数组
	struct Teacher tArray[3];
	
	//通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
int len=sizeof(tArray)/sizeof(tArray[0]);
allocateSpace(tArray,len);
		//打印所有老师及所带的学生信息
	printInfo(tArray,len);
	system("pause");
	return 0;
}

引用

#include <iostream>
using namespace std;
int main()
{
	int a=10;
	//1,引用必须初始化
	//int &b;//错误,必须要初始化
	int &b=a;
	//2引用在初始化后,不可以改变
	int c=20;
	b=c;
	cout<<a<<b<<c;
	system("pause");
	return 0;
}

多态

/*多态是c++面向对象三大特征之一
多态分为两类
1.静态多态:函数重载和运算符重载属于静态多态,副用函数名
2.动态多态:派生类和虚函数实现运行时多态
静态多态和动态多态区别
静态多态的函数地址早绑定 编译阶段确定函数地址
动态多态的函数地址晚绑定,运行阶段确定函数地址
*/
#include <iostream>
using namespace std;
//多态 
class Animal
{
	public:
	//虚函数
	 virtual void speak()
	{
		cout<<"动物在说话"<<endl;//此非静态成员,函数不属于类的对象上面,
	}//分开存储
};
//猫类
class Cat:public Animal
{
	public:
	//重写 函数返回值类型 函数名 参数列表 完全相同
	void speak()
	{
		cout<<"小猫在说话"<<endl;
	}
};
//狗类
class Dog:public Animal
{
	public:
	void speak()
	{
		cout<<"小狗在说话"<<endl;
	}
};
//执行说话的函数
//地址早绑定 在编译阶段确定函数地址
//如果想执行让猫说话,那么这个函数地址就不能提前绑定,
//需要在运行阶段进行绑定,地址晚绑定
//动态多态满足条件
//1.有继承关系
//2.子类要重写父类的虚函数
//动态多态的使用
//**父类的指针或者引用 指向子类对象**
void doSpeak(Animal &animal)//Animal & anlmal=cat;
{
	animal.speak();
}
void test01()
{
	Cat cat;
	doSpeak(cat);
	Dog dog;
	doSpeak(dog);
}
/*void test02()
{
	cout<<"sizeof animal="<<sizeof(Animal)<<endl;
}*/
int main()
{
	test01();
	//test02();
	system("pause");
	return 0;
}

多态2-煮茶

#include <iostream>
using namespace std;
class AbstructDrinking
{
	public:
	//煮水
	virtual void Boil()=0;
	//冲泡
	virtual void Brew()=0;
	//倒入杯中
	virtual void PourInCup()=0;
	//加入辅料
	virtual void PutSomething()=0;
	//制作饮品
	void makeDrink()
	{
		Boil();
		Brew();
		PourInCup();
		PutSomething();
	}
};
class Coffee:public AbstructDrinking
{
	public:
		//煮水
	 void Boil()
	{
		cout<<"煮水"<<endl;
	}
	//冲泡
	virtual void Brew()
	{
		cout<<"冲泡咖啡"<<endl;
	}
	//倒入杯中
	virtual void PourInCup()
	{
		cout<<"倒入杯中"<<endl;
	}
	//加入辅料
	virtual void PutSomething()
	{
		cout<<"加入糖和牛奶"<<endl;
	}
};
//制作茶叶
class Tea:public AbstructDrinking
{
	public:
		//煮水
	virtual void Boil()
	{
		cout<<"煮矿泉水"<<endl;
	}
	//冲泡
	virtual void Brew()
	{
		cout<<"冲茶叶"<<endl;
	}
	//倒入杯中
	virtual void PourInCup()
	{
		cout<<"倒入玻璃杯中"<<endl;
	}
	//加入辅料
	virtual void PutSomething()
	{
		cout<<"加入柠檬"<<endl;
	}
};
//制作的函数
void doWork(AbstructDrinking *abs)//AbstructDrinking*abs=new Coffee
{
	abs->makeDrink();
	delete abs;
}
	
void test01()
{
	//制作咖啡
	doWork(new Coffee);
	cout<<"----------"<<endl;
	doWork(new Tea);
}
	
int main()
{
test01();
	system("pause");
	return 0;
}

多态-电脑组装

#include <iostream>
using namespace std;

//抽象不同零件类
//抽象CPU类
class CPU
{
	public:
	//抽象的计算函数
	virtual void calculate()=0;
};
//抽象显卡类
class VideoCard
{
	public:
	//抽象的显示函数
	virtual void display()=0;
};
//抽象内存条类
class Memory
{
	public:
	//抽象的计算函数
	virtual void storage()=0;
};
//电脑类
class Computer
{
	public:
	Computer(CPU *cpu,VideoCard*vc,Memory *mem)
	{
		m_cpu=cpu;
		m_vc=vc;
		m_mem=mem;
	}
	//提供一个工作的函数
	void work()
	{
		m_cpu->calculate();
		m_vc->display();
		m_mem->storage();
	}
	//提供析构函数 释放3个电脑零件
	~Computer()
	{
		if(m_cpu !=NULL)
		{
			//释放CPU零件
			delete m_cpu;
			m_cpu=NULL;
		}
			if(m_vc !=NULL)
		{
			//释放显卡零件
			delete m_vc;
			m_vc=NULL;
		}
			if(m_vc !=NULL)
		{
			//释放内存条零件
			delete m_mem;
			m_mem=NULL;
		}
	}
	
	private:
	CPU*m_cpu;//CPU的零件指针
	VideoCard *m_vc;//显卡的零件指针
	Memory*m_mem;//内存条零件指针
};
//具体厂商
//Inter厂商
class IntelCPU:public CPU
{
	public:
	virtual void calculate()
	{
		cout<<"Inter的cpu开始计算了"<<endl;
	}
};
class IntelVideoCard:public VideoCard
{
	public:
	virtual void display()
	{
		cout<<"Inter的显卡开始显示了"<<endl;
	}
};
class IntelMemory:public Memory
{
	public:
	virtual void storage()
	{
		cout<<"Inter的内存条开始显示了"<<endl;
	}
};
//Lenovo厂商
class LenovoCPU:public CPU
{
	public:
	virtual void calculate()
	{
		cout<<"Lenovo的cpu开始计算了"<<endl;
	}
};
class LenovoVideoCard:public VideoCard
{
	public:
	virtual void display()
	{
		cout<<"Lenovo的显卡开始显示了"<<endl;
	}
};
class LenovoMemory:public Memory
{
	public:
	virtual void storage()
	{
		cout<<"Lenovo的内存条开始显示了"<<endl;
	}
};
void test01()
{
	//第一台电脑零件
	CPU*intelCpu=new IntelCPU;
	VideoCard *intelCard=new IntelVideoCard;
	Memory *intelMem=new IntelMemory;
	//创建第一台电脑
	Computer *computer1=new Computer(intelCpu,intelCard,intelMem);
	computer1->work();
	delete computer1;
	cout<<"------------"<<endl;
	cout<<"第二台电脑开始工作"<<endl;
	//第二台电脑的组装
	Computer *computer2=new Computer(new LenovoCPU,new LenovoVideoCard,new LenovoMemory);
	computer2->work();
delete computer2;
	//第三台电脑的组装
	cout<<"第三条电脑的组装"<<endl;
	Computer *computer3=new Computer(new IntelCPU,new LenovoVideoCard,new IntelMemory);
	computer3->work();
delete computer3;
}
int main()
{
	test01();
	//test02();
	system("pause");
	return 0;
}

文件操作

#include <iostream>
using namespace std;
#include<fstream>//头文件包含
//文本文件 写文件
int main()
{
	//1,包含头文件 fstream
	
	//2,创建流对象
	
	ofstream ofs;
	
	//3,指定打开方式
	ofs.open("test.txt",ios::out);
	
	//4,写内容
	ofs<<"姓名:张三"<<endl;
		ofs<<"姓别:男"<<endl;
			ofs<<"年龄:18"<<endl;
			//5,关闭文件
			ofs.close();
	system("pause");
	return 0;
}

读文件

#include <iostream>
using namespace std;
#include<fstream>
#include<string>
//文本文件 读文件
void test01()
{
	//1,包含头文件
	
	//2.创建流对象
	ifstream ifs;
	//3.打开文件 并且判断是否打开成功
	ifs.open("test.txt",ios::in);
	if( !ifs.is_open())
	{
		cout<<"文件打开失败"<<endl;
		return;
	}

	//4,读数据
	//第一种
	char buf[1024]={0};
	while(ifs>>buf)
	{
		cout<<buf<<endl;
	}
	//第二种
	/*
	char buf[1024]={0};
	while (ifs.getline(buf,sizeof(buf)))
	{
		cout<<buf<<endl;
	}
	//第三种
	string buf;
	while(getline(ifs,buf))
	{
		cout<<buf<<endl;
	}*/
	
	//5关闭文件
	ifs.close();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

案例文件写

#include <iostream>
using namespace std;
#include<fstream>
int main()
{
	ofstream gjl;
	gjl.open("miji.txt",ios::out);
	gjl<<"秘籍一:无敌"<<endl;
	gjl<<"秘籍二;无限体力"<<endl;
	gjl<<"秘籍三:无限火龙"<<endl;
	gjl.close();
	system("pause");
	return 0;
}

案例文件读

#include <iostream>
using namespace std;
#include<fstream>
#include<string>
void test01()
{
	ifstream ifs;
	ifs.open("miji.txt",ios::in);
	if(!ifs.is_open())
	{
		cout<<"文件输出失败"<<endl;
		return;
	}
	char buf[1024]={0};
	while(ifs>>buf)
	{
		cout<<buf<<endl;
	}
	ifs.close();
}
	int main()
	{
		test01();
		system("pause");
		return 0;
	}

模板1

#include <iostream>
using namespace std;
#include<string>
//类模板
template<class NameType,class AgeType>
class Person
{
	public:
	Person(NameType name,AgeType age)
	{
		this->m_Name=name;
		this->m_Age=age;
	}
	void showPerson()
	{
		cout<<"name="<<this->m_Name<<"age:"<<this->m_Age<<endl; 
	}
		NameType m_Name;
		AgeType m_Age;
		
 };
 void test01()
 {
 	Person<string,int>p1("孙悟空",999);
 	p1.showPerson();
 }
int main()
{
	test01(); 
	system("pause");
	return 0;
 } 

模板2

#include <iostream>
using namespace std;
void swapNum(int &a,int &b)
{
	int temp=a;
	a=b;
	b=temp;
}
void swapDouble(double &a,double &b)
{
	double temp=a;
	a=b;
	b=temp;
}
//函数模板
template<typename T>//声明一个模板,告诉编译器后面代码中紧跟着
//的T不要报错,T是一个通用数据类型
void mySwap(T&a,T&b)
{
	T temp=a;
	a=b;
	b=temp;
}
void test01()
{
	int a=10;
	int b=20;
//	swapNum(a,b);
//利用函数模板进行交换
//有两种方式使用模板
//1,自动类型推导
//mySwap(a,b);
//2.显示指定类型
mySwap<int>(a,b);
	cout<<"a="<<a<<"b="<<b<<endl;
	/*double c=1.1;
	double d=2.2;
	swapDouble(c,d);
		cout<<"c="<<c<<"d="<<d<<endl;*/
}
int main()
{
	test01();
	system("pause");
	return 0;
}

模板注意事项

#include <iostream>
using namespace std;
//函数模板注意事项
template<typename T>//typename 可以替换成class
void mySwap(T&a,T&b)
{
	T temp=a;
	a=b;
	b=temp;
}
//1,自动类型推导,必须推导出一致的数据类型T才可以使用
void test01()
{
	int a=10;
	int b=20;
	char c='c';
	mySwap(a,b);//正确
	//mySwap(a,c);//错误,推导不出一致的T类型
	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;
}
//2.模板必须要确定出T的数据类型,才可以使用
template<class T>
void fun()
{
	cout<<"fun调用"<<endl;
}
void test02()
{
	fun<int>();
	
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

函数模板

#include <iostream>
using namespace std;
//实现通用 对数组进行排序的函数
//规则 从大到小
//算法 选择排序
//测试 char数组,int数组
//交换函数模板
template<class T>
void mySwap(T&a,T&b)
{
	T temp=a;
	a=b;
	b=temp;
}
//排序的数组
template<class T>
void mySort(T arr[],int len)
{
	for(int i=0;i<len;i++)
	{
		int max=i;//认定最大值的下标
		for(int j=i+1;j<len;j++)
		{//认定的最大值比遍历出的数值要小,说明j下标的元素才是最大值
			if(arr[max]<arr[j])
			{
				max=j;//更新最大值下标
				}
		}
		if(max!=i)
		{
			//交换max和i元素
			mySwap(arr[max],arr[i]);
		}
	}
}
//提供打印数组的模板
template<class T>
void printArray(T arr[],int len)
{
	for(int i=0;i<len;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
}
void test01()
{
	//测试char数组
	char charArr[]="badcef";
	int num=sizeof(charArr)/sizeof(char);
	mySort(charArr,num);
	printArray(charArr,num);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

普通函数与函数模板的区别

//普通函数与函数模板的区别
//1.普通函数调用时可以发生自动类型转换(隐式类型转换)
//2.函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
//3.如果利用显示指定类型的方式,可以发生隐式类型转换

模板规则

#include <iostream>
using namespace std;
//普通函数和函数模板的调用规则
//1.如果函数模板和普通函数都可以调用,优先调用普通函数
//2.可以通过空模板参数列表 强制调用 函数模板
//3.函数模板可以发生函数重载
//4.如果函数模板可以产生更好的匹配,优先调用函数模板
void myPrint(int a,int b)
{
	cout<<"调用普通函数"<<endl; 
  } 
 template<class T>
 void myPrint(T a,T b)
 {
 	cout<<"调用的模板"<<endl; 
 }
  template<class T>
 void myPrint(T a,T b,T c)
 {
 	cout<<"调用的重载的模板"<<endl; 
 }
void test01()
{
	int a=10;
	int b=20;
	//myPrint(a,b);
	myPrint(a,b,100);
	//如果函数模板产生更好的匹配,优先调用函数模板
	char c1='a';
	char c2='b';
	myPrint(c1,c2); 
	//通过空函数参数模板,强制调用函数模板
	myPrint<>(a,b); 
}
int main()
{
	test01();
	
	system("pause");
	return 0;
}

模板的局限性

#include <iostream>
using namespace std;
#include<string>
//模板的局限性
//模板不是万能的,有些特定数据类型,需要用具体方式做特殊实现
class Person
{
	public:
		Person(string name,int age)
		{
			this->m_Name=name;
			this->m_Age=age;
		}
	public: 
	//姓名
	string m_Name;
	//年龄 
	int m_Age;
};
//对比两个数是否相等
template<class T>
bool myCompare(T &a,T &b)
{
	if(a==b)
	{
		return true;
	}
	else
	{
		return false;
	}
 } 
 //利用具体化Person的版本实现代码,具体化优先调用
template<> bool myCompare(Person &p1,Person &p2)
{
	if(p1.m_Name==p2.m_Name&&p1.m_Age==p2.m_Age)
	{
		return true;
	}
	else
	{
		return false;
	}
 } 
/* void test01()
 {
 	int a=10;
 	int b=20;
 	bool ret=myCompare(a,b);
 	if(ret)
 	{
 		cout<<"a==b"<<endl;
	 }
	 else
	 {
	 	cout<<"a!=b"<<endl;
	 }
 }*/
void test02()
{
	Person p1("tom",12);
	Person p2("tom",12);
	bool ret=myCompare(p1,p2);
	if(ret)
	{
		cout<<"p1==p2"<<endl;
	}
	else
	{
		cout<<"p1!=p2"<<endl;
	}
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
 } 

类模板与函数模板的区别

#include <iostream>
using namespace std;
#include <string>
//类模板和函数模板的区别
template<class NameType,class AgeType=int>
class Person
{
	public:
		Person(NameType name,AgeType age)
		{
			this->m_Name=name;
			this->m_Age=age; 
		 } 
		 void showPerson()
		 {
		 	cout<<"name="<<this->m_Name<<endl;
		 	cout<<"age="<<this->m_Age<<endl;
		 }
		NameType m_Name;
		AgeType m_Age;
 };
 //1.类模板没有自动类型推导使用方式
 void test01()
 {
 //	Person p("孙悟空",1000);错误,无法用显示类型推导
 Person<string,int>p("孙悟空",1000);//正确,只能用显示指定类型
  p.showPerson();
  } 
 
 //2.类模板在模板参数列表中可以有默认参数 
 void test02()
 {
 	Person<string>p("猪八戒",999);
	 p.showPerson(); 
 }
int main()
{
	void test01();
	system("pause");
	return 0;
 } 

成员函数创建时期

#include <iostream>
using namespace std;
//类模板中成员函数创建时机
//类模板中成员函数在调用时才去创建
class Person1
{
	public:
		void showPerson1()
		{
			cout<<"Person1 show"<<endl;
		}
 };
 class Person2
{
	public:
		void showPerson2()
		{
			cout<<"Person2 show"<<endl;
		}
 };
 template<class T>
 class MyClass
 {
 	public:
 		T obj;
 			//类模板中的成员函数
			 void fun1()
			 {
			 	obj.showPerson1();
			  }
			  void fun2()
			  {
			  	obj.showPerson2();
			   } 
			  
  };
  void test01()
  {
  	MyClass<Person1>m;
  	m.fun1();
   //  m.fun2();
  }
int main()
{
	test01();
	system("pause");
	return 0;
 } 

类模板成员函数类外实现

#include <iostream>
using namespace std;
#include<string>
//类模板成员函数类外实现
template<class T1,class T2>
class Person
{
	public:
	Person(T1 name,T2 age);
/*	{
		this->m_Name=name;
		this->m_Age=age;
	}*/
	void showPerson();
/*	{
		cout<<"姓名:"<<this->m_Name<<"年龄:"<<this->m_Age<<endl;
	}*/
	T1 m_Name;
	T2 m_Age;
};
//构造函数类外实现
template<class T1,class T2>
Person<T1,T2>::Person(T1 name,T2 age)
{
	this->m_Name=name;
	this->m_Age=age;
}
//成员函数类外实现
template<class T1,class T2>
void Person<T1,T2>::showPerson()
{
	cout<<"姓名:"<<this->m_Name<<"年龄:"<<this->m_Age<<endl;
}
void test01()
{
	Person<string,int>P("Tom",20);
	P.showPerson();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

类模板与继承

/*类模板与继承
当子类继承的父类是一个类模板时,子类在声明的时候
要指定出父类中T的类型
2.如果不指定,编译器无法给子类分配内存
3.如果想灵活给出父类中T的类型,子类也需变为类模板*/
#include <iostream>
using namespace std;
//类模板与继承
template<class T>
class Base
{
	T m;
};
//class son:public Base//错误,必须要知道父类中T的数据类型,
                                                                           //才能继承给子类
  class son:public Base<int>
{
	
};
void test01()
{
	son s1;
}
//如果想灵活指定父类中T类型,子类也需要变类模板
template<class T1,class T2>
class son2:public Base<T2>
{
	public:
	son2()
	{
		cout<<"T1的类型为:"<<typeid(T1).name()<<endl;
			cout<<"T2的类型为:"<<typeid(T2).name()<<endl;
	}
	T1 obj;
};
void test02()
{
	son2<int,char>s2;
}
int main()
{
//	test01();
test02();
	system("pause");
	return 0;
}

设计一个卡车类

#include <iostream>
//#include<cstream>
using namespace std;
class Vehicle
{
public:
	virtual void showinfo() = 0;
protected:
	char Name[20];
};
class Car :public Vehicle
{
public:
	Car(char *name)
	{
		strcpy(Name, name);
	}
	void showinfo()
	{
		cout << Name << endl;
	}
protected:
	int Radius;
};
class Truck :public Vehicle
{
public:
	Truck(char*name)
	{
		strcpy(Name, name);
	}
	void showinfo(){ cout << Name << endl; }
};
class Boat :public Vehicle
{
public:
	Boat(char*name)
	{
		strcpy(Name, name);
	}
	void showinfo(){ cout << Name << endl; }
};
int main()
{
	Vehicle*vp;
	Car car("奔驰");
	Truck truck("运输卡车");
	Boat boat("游艇");
		vp = &car;
	vp->showinfo();
	vp = &truck;
	vp->showinfo();
	vp = &boat;
	vp->showinfo();
	return 0;

}
  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

煎饼果子小鸢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值