一、友元函数
可以访问私有
#include <iostream>//包含了大部分的头文件 using namespace std;//命名空间相当于全局声明std 输出流 class Car //类名 { public: //公有 friend void Print(Car *sp);//修饰成为友元函数 void h(int yy)//改变私有变量 { number=yy; } private://私有数据成员和成员函数 int H=170;ll身高 int number=1000;//身价 }; /** * @brief Print * param sp对象传参时必须传这个类型的对象 */ void Print(Car *sp)l/公共访问私有变量 { cout<<"身高"<<sp->H<<"CM"<<endl; cout<<"身价"<< sp->number <<"万"<<endl; } int main() { Car *Pt =new Car( ;Pt->h( 100000000);Print(Pt); } //类的继承,函数多态,函数重载
二、类的嵌套
#include <iostream>//头文件 #include <string.h> using namespace std;//命名空间 class Animal //动物类型 { public: //公有的 int a=1000; class Dog// { public: int b=999; }; }; int main() { Animal sp; cout <<sp.a<<endl; Animal : :Dog st;//访问Dog必须加上作用域 否则禁止访问 st.b;//访问嵌套的成员 cout <<st.b<<endl; return 0; }
带参数的嵌套
#include <iostream> #include <string> using namespace std; class Boy { private : public: Boy (); class Human { private: int height; int weight; public: Human(int height,int weight); //声明构造函数 ~Human( ); }; }; Boy : : Human: : Human(int height , int weight) :height(height),weight(weight) { cout << height ; } Boy : : Human : : ~Human()//析构函数 { } int main(void) { //Human a; //Human类作为Boy类下的嵌套类,不能只用该类的类名创建对象 Boy :: Human a(10,199);//使用类名::嵌套类名对象 来创建嵌套类的对象 }
三、命名空间
封装变量函数
#include <iostream> #include <string> using namespace std;//没有命名空间需要加上输出流的作用域 //定义带标签的全局定义 namespace Usart{ int bound=115200; int datalen; int TXD; int RXD; void fun() { std: : cout<<"12345"<<std : :endl; } } using namespace Usart; //声明 int main(void) { std:: cout<<Usart: : bound<<std : :endl; Usart:: fun(); } //inline 内联函数自己写的函数有时候会重复嵌套使用,会造成多次重复执行和编译
四、继承和多继承
例子:
换代叠加功能,重写优化
单继承格式:
class 派生类名∶派生方式 基类名
{ // 派生类新增的数据成员和成员函数 } ;
多继承:
class 派生类名∶派生方式 基类A名,派生方式 基类B名,
{ // 派生类新增的数据成员和成员函数 } ;
派生出来的类就可以使用公共函数。。。。。
#include <iostream>//包含了大部分的头文件 using namespace std; //命名空间相当于全局声明std输出流 class Car //基类父类 { public:l//公有 void Cou() { cout<<"基类函数:"<<"4个轮子"<<endl; } private: //私有数据成员和成员函数 int x=100; }; class A //基类父类 { public: //公有 void couA() { cout<<"A类: "<<"外壳"<<endl;} }; //子类派生类 class Car_v10 :public car,public A { public: //公有 void couV10() { cout<<"子类函数;"<<"4个轮子+发动机"<<endl; } private: //私有数据成员和成员函数 }; int main() { Car_v10 *sp= new Car_v10(); sp->Cou(); sp->Couv10(); sp->CouA(); }
私有:
1、私有派生
(1)私有派生类对基类成员的访问
由私有派生得到的派生类,对它的基类的公有成员只能是私有继承,也就是说基类的所有公有成员都只能成为私有派生的私有成员,这些私有成员能够被派生的成员函数访问,但是基类私有成员不能被派生类成员函数访问。
五、函数重载
<< >> 在C语言叫左移右移 在C++可以是输入输出也可以是左移右移
意思就是说一个东西多种功能 ,在C语言函数是不能重名的
函数重载:函数名一样 参数根据形参而选择函数使用,函数重载一次后在派生类内不要在次重载
#include <iostream>//包含了大部分的头文件 using namespace std; //命名空间相当于全局声明std 输出流 class Car //基类父类 { public://公有 void Cou() { cout<<"无参数"<<endl; } void Cou(int a) { cout<<"输出整型"<<a<<endl; } void Cou(string a) { cout<<"字符串"<<a<<endl; } private: //私有数据成员和成员函数 }; //子类派生类 class Car_v10 :public Car { public: //公有 void Couv10() { cout<<"子类函数: "<<"4个轮子+发动机"<<endl; } private: //私有数据成员和成员函数 }; int main() { Car_v10*sp= new Car_v10(); sp->Cou(); sp->Cou( 10); sp->Cou("你好"); sp->CouV10() ; }
六、函数多态
函数多态作用,调用不同对象对同意函数操作会输出不同结果,为了后续重写,抽象函数
#include <iostream>//包含了大部分的头文件 using namespace std;//命名空间相当于全局声明std 输出流 class Person { public: //虚函数 virtual void BuyTicket(){/*空*/} protected: string _name; }; class student : public Person { public: //虚函数+函数名/参数/返回值-》重写/覆盖 virtual void BuyTicket() { cout <<_name << "student:买票-半价50 ¥" <<endl; } }; int main() { Person *sp=new Person(); sp->BuyTicket(); Student * pt =new Student(); pt->BuyTicket(); }
通过不用对象执行不同的虚函数
#include <iostream>/( 包含了大部分的头文件 using namespace std;//命名空间相当于全局声明std输出流 class Person { public: //虚函数 virtual void BuyTicket() { cout <<_name <<"Person:买票-全价100¥" <<endl; } protected : string _name; }; class Student : public Person { public: //虚函数+函数名/参数/返回值-》重写/覆盖 virtual void BuyTicket() { cout <<_name << "student:买票-半价50 ¥" << endl; } }; int main() { Person *sp=new Person(); sp->BuyTicket(); Student * pt =new student(); pt->BuyTicket(); }
重写析构函数
#include <iostream>//包含了大部分的头文件 using namespace std;//命名空间相当于全局声明std 输出流 class Person { public: virtual ~Person() {cout <<"~Person()" <<endl;} }; class Student : public Person { public: virtual ~Student() { cout <<"~Student()" <<endl; } }; int main() { Person* p1 = new Person ; Person* p2 = new Student; delete p1; delete p2 ; return 0; }
防止重写虚函数
#include <iostream>//包含了大部分的头文件 using namespace std;//命名空间相当于全局声明std输出流 class Person { public: //虚函数 virtual void BuyTicket() final //声明接口函数不被重写 { cout <<_name << "Person:买票-全价100¥" <<endl; } protected : string _name; }; class student : public Person { public: int main() { Person *sp=new Person(); sp->BuyTicket(); student * pt =new student(); pt->BuyTicket(); }
跟C语言一样:
#include "stdio.h"
int main()
{
const int a;//只读
a = 100;
}
七、函数模板
函数模板跟函数重载类似,区别在于函数模板的类型不确定,还可以强制转换类型:-----泛型编程
template <typename T> T Myswap(const T a,const T b) { T sp = a+b; return sp; } int mian() { int a = 90 ; int ss = 7; string str1 = " 你好!"; string str2 = " 中国"; cout<< Myswap< char> (a,ss) << endl; //传入参数强转为char cout<< Myswap(a,ss)<<endl; //默认为int类型 cout<< Myswap(str1,str2)<<endl; //默认额字符串类型 }
String类的简单使用:
#include <iostream> using namespace std ; int main() { int a=90; int ss=7; string str1="一>你好! "; string str2="中国"; cout << a+ss<<endl; cout <<str1+str2<<endl; if("中国"==str2) { cout <<"比较"<<endl; } str1+="很好";//拼接功能 cout <<"拼接"<<str1<<endl; cout<<"跟数组一样单个元素访问"<<str1.at(o)<<endl;l str1.append ( ">>>>") ; cout <<"插入"<<str1<<endl ; str1.clear ( ) ; cout <<"清空:"<<str1<<endl; }
八、带参数的构造函数继承
#include <iostream> using namespace std; class A { public:A(int s){ cout<< "s="<<s<<endl; } }; classB: public A { public : B(int x):A(x) //A(x)传什么值给Xx就把值传给A的的构造函数 { cout<< "X"<<x; } }; int main() { A *sp =new A(88) ; B*pt =new B( 100 ) ; }
另有给私有变量传参
#inc1ude <iostream>//头文件 #inc1ude <string.h> using namespace std;//命名空间 class Anima1 //动物类型 { pub1ic://公有的 Anima1(int x) :a(x)//直传X的值给acl{J }; void prin(void) { cout <<a ; } private://私有-int a=999;//私有的- ( }; int main() { Anima1 *Dog = new Anima1(100) ; //new形式创建一个对象纠Dog->prin( ; delete Dog;/ /释放对象return 0; }