1.构造函数与析构函数
cout << Test::HelloStr << endl; // 通过作用域运算符访问
Test::HelloStr = "World"; // 修改静态变量 HelloStr
Test t1;
cout << t1.HelloStr <<endl; // 通过对象访问
Test::Hello(); // 通过作用域运算符访问
t1.World(); // 通过对象访问
class User{
public:
string Name;
int Books=0;
static int tot_books,tot_ren;
User(string name,int books):Name(name),Books(books){
cout<<Name<<" "<<Books<<" 进入"<<endl;
tot_books+=Books;
tot_ren++;
};
~User(){
cout<<Name<<" "<<Books<<" 离开"<<endl;
tot_books-=Books;
tot_ren--;
};
static void GetState(){
cout<<"书店人数:"<<tot_ren<<",书店共享书数量:"<<tot_books<<",人均共享数量:"
<<tot_books/tot_ren<<endl;
}
};
int User::tot_books=0;
int User::tot_ren=0;
2.内部类练习
/* Cow a(weight,height);
a.display(color);
Cow::CowLeg leg(color, height);
leg.show(); */
class Cow{
public:
int weight,height;
Cow(int _weight,int _height):weight(_weight),height(_height){};
class CowLeg{
public:
string color;
int length;
CowLeg(string _color,int _length):color(_color),length(_length){};
void display(string _color){
color=_color;
}
void show(){
cout<<"牛腿的颜色是"<<color<<endl;
cout<<"牛腿的长度是"<<length<<endl;
}
};
CowLeg leg;
~Cow(){
leg.show();
cout<<"整牛高"<<height<<endl;
cout<<"整牛重"<<weight<<endl;
};
3.类的书写(运算符重载)
3.1复数
class Complex{
public:
double real,image;
Complex(double r,double i):real(r),image(i){};
void Print(){
if(image>=0) cout<<real<<"+"<<image<<"i"<<endl;
else cout<<real<<image<<"i"<<endl;
}
Complex operator + (const Complex tem){
return (Complex){real+tem.real, image+tem.image};
}
Complex operator - (const Complex tem){
return (Complex){real-tem.real, image-tem.image};
}
Complex operator * (const Complex tem){
//(a+bi)(c+di)=(ac-bd)+(ad+bc)i;
return (Complex){real*tem.real-image*tem.image, real*tem.image+tem.real*image};
}
Complex operator / (const Complex tem){
double a=real,b=image,c=tem.real,d=tem.image;
//(a+bi)(c-di)/(c^2+d^2)=[(ac+bd)+(bc-ad)i]/(c^2+d^2);
return (Complex){(a*c+b*d)/(c*c+d*d),(b*c-a*d)/(c*c+d*d)};
}
};
4.友元函数与友元类
4.1友元函数
friend <返回类型> <函数名> (<参数列表>);
class Test{
private:
int a;
protected:
int b;
public:
friend void fun(Test t); // 友元函数
};
void fun(Test t) {
t.a = 100;
t.b = 2000;
cout << "a = " << t.a << ", b = " << t.b << endl;
}
int main() {
Test test;
fun(test);
return 0;
}
4.2友元类
class TestFriend {
private:
int a;
}; // TestFriend 类的声明在 Test 类之前
class Test {
private:
int a;
friend class TestFriend; // 声明友元类
};
class TestFriend; // 前置声明 TestFriend 类,只是一个名字
class Test {
private:
int a;
friend class TestFriend; // 声明友元类
// 尝试将 TestFriend 类的成员函数 Func 作为友元函数,
但由于 TestFriend 类目前只有前置声明,所以会出错。
friend void TestFriend::Func();
};
class TestFriend // TestFriend 类的声明在 Test 类之后
{
private:
int a;
public:
void Func();
};
#include <iostream>
#include <string>
using namespace std;
class Teacher;
class Student{
private:
int number;
string name,sex;
public:
Student(int num,string nam,string se):number(num),name(nam),sex(se){};
void Print(){
cout<<"学生:"<<name<<",编号:"<<number<<",性别:"<<sex<<endl;
}
friend class Teacher;
};
class Teacher{
private:
int number;
string name,sex;
public:
void Print(){
cout<<"教师:"<<name<<",编号:"<<number<<",性别:"<<sex<<endl;
}
Teacher(const Student &a):number(a.number),name(a.name),sex(a.sex){};
};
4.3应用(矩阵)