南充外贸网站建设,网站建设的税率,哪个汽车网站好,博客网站入口一、Xmind整理#xff1a; 二、上课笔记整理#xff1a;
1.静态数据成员静态成员函数#xff08;银行账户实例#xff09;
#include iostreamusing namespace std;class BankAccount
{
private:double balance; //余额static double interest_rate; //利率
p…一、Xmind整理 二、上课笔记整理
1.静态数据成员静态成员函数银行账户实例
#include iostreamusing namespace std;class BankAccount
{
private:double balance; //余额static double interest_rate; //利率
public://无参构造函数BankAccount (){}//有参构造函数BankAccount(double balance):balance(balance){}//静态成员函数,获取当前利率static double getInterestRate(){return interest_rate;}//静态成员函数设置当前利率、static double setInterestRate(double rate){interest_rate rate;}//静态成员函数获取连本带利的全额static double getLastMoney(BankAccount account){return account.balance*(1interest_rate);}
};double BankAccount::interest_rate 0.05;int main()
{cout 当前利率 BankAccount::getInterestRate() endl;BankAccount::setInterestRate(0.03);cout 当前利率 BankAccount::getInterestRate() endl;BankAccount account1(2000.0);BankAccount account2(3000.0);cout 第一个人连本带利的余额为 BankAccount::getLastMoney(account1) endl;return 0;
}2.类的继承
#include iostreamusing namespace std;//封装 人 类 父类/基类
class Person
{
private:string name;
protected:int age;
public:int h;
public://无参构造函数Person(){cout 父类的无参构造函数 endl;}//有参构造Person(string name, int age, int h):name(name),age(age),h(h){cout 父类的有参构造函数 endl;}//拷贝构造函数Person(const Person other):name(other.name),age(other.age),h(other.h){cout 父类的拷贝构造函数 endl;}//拷贝赋值函数Person operator(const Person p){name p.name;age p.age;h p.h;cout 父类的拷贝赋值函数 endl;return *this;}void show(){cout 父类的show endl;}
};//封装 学生 类 共有继承人 类
class Stu:public Person //子类 、派生类
{
private:int id;int math;public://无参构造函数Stu(){cout 子类的无参构造函数 endl;}//有参构造函数Stu(string name, int age, int h, int id, int math):Person(name,age,h),id(id),math(math){cout 子类的有参构造函数 endl;}//拷贝构造函数Stu(const Stu s):id(s.id),math(s.math),Person(s){cout 子类的拷贝构造函数 endl;}//拷贝赋值函数Stu operator(const Stu s){Person::operator(s);id s.id;math s.math;cout 子类的拷贝赋值函数 endl;return *this;}void show(){cout 子类的show endl;cout h endl; //通过共有继承类外、子类可以访问父类共有成员cout age endl; //通过共有继承子类可以访问父类保护成员,类外不可以访问//cout name endl;//通过共有继承子类不可访问父类私有成员,类外不可以访问}
};int main()
{Stu s(zhangsan,12,190,1001,78);Stu s2s;Stu s3;s3 s2;// s.show();
// s.Person::show();return 0;
}
3.多继承
#include iostreamusing namespace std;class Sofa
{
private:string sitting;
public://无参构造函数Sofa(){cout 沙发的无参构造 endl;}//有参构造函数Sofa(string s):sitting(s){cout 沙发的有参构造 endl;}void display(){cout sitting endl;}
};class Bed
{
private:string sleep;
public://无参构造函数Bed(){cout 床的无参构造 endl;}//有参构造函数Bed(string(s)):sleep(s){cout 床的有参构造 endl;}void display(){cout sleep endl;}
};class Sofa_Bed:public Bed,public Sofa
{
private:int w;
public://无参构造函数Sofa_Bed(){cout 沙发床的无参构造 endl;}//有参构造函数Sofa_Bed(string sit, string s, int w):Bed(s),Sofa(sit),w(w){cout 沙发床的有参构造 endl;}
};
int main()
{Sofa_Bed(s);Sofa_Bed s1(可坐,可躺,123);s1.Sofa::display();s1.Bed::display();return 0;
}