思维导图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e242a307acb0ab1af5d97bab8bd51385.png)
代码示例:
#include <iostream>
#include <cmath>
using namespace std;
class Point {
public:
void initPoint(float x = 0, float y = 0) { this->x = x; this->y = y;}
void move(float offX, float offY) { x += offX; y += offY; }
float getX() const { return x; }
float getY() const { return y; }
private:
float x, y;
};
class Rectangle: public Point {
public:
void initRectangle(float x, float y, float w, float h) {
initPoint(x, y);
this->w = w;
this->h = h;
}
float getH() const { return h; }
float getW() const { return w; }
private:
float w, h;
};
int main() {
Rectangle rect;
rect.initRectangle(2, 3, 20, 10);
rect.move(3,2);
cout << "The data of rect(x,y,w,h): " << endl;
cout << rect.getX() <<", "
<< rect.getY() << ", "
<< rect.getW() << ", "
<< rect.getH() << endl;
system("pause");
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
class Point {
public:
void initPoint(float x = 0, float y = 0) { this->x = x; this->y = y;}
void move(float offX, float offY) { x += offX; y += offY; }
float getX() const { return x; }
float getY() const { return y; }
private:
float x, y;
};
class Rectangle: private Point {
public:
void initRectangle(float x, float y, float w, float h) {
initPoint(x, y);
this->w = w;
this->h = h;
}
void move(float offX, float offY) { Point::move(offX, offY); }
float getX() const { return Point::getX(); }
float getY() const { return Point::getY(); }
float getH() const { return h; }
float getW() const { return w; }
private:
float w, h;
};
int main() {
Rectangle rect;
rect.initRectangle(2, 3, 20, 10);
rect.move(3,2);
cout << "The data of rect(x,y,w,h): " << endl;
cout << rect.getX() <<", "
<< rect.getY() << ", "
<< rect.getW() << ", "
<< rect.getH() << endl;
system("pause");
return 0;
}
class A {
protected:
int x;
};
int main() {
A a;
a.x = 5;
}
class A {
protected:
int x;
};
class B: public A{
public:
void function();
};
void B:function() {
x = 5;
}
#include <iostream>
#include <cmath>
using namespace std;
class A {
public:
void setA(int);
void showA() const;
private:
int a;
};
class B {
public:
void setB(int);
void showB() const;
private:
int b;
};
class C : public A, private B {
public:
void setC(int, int, int);
void showC() const;
private:
int c;
};
void A::setA(int x) {
a=x;
}
void B::setB(int x) {
b=x;
}
void C::setC(int x, int y, int z) {
setA(x);
setB(y);
c = z;
}
int main() {
C obj;
obj.setA(5);
obj.showA();
obj.setC(6,7,9);
obj.showC();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class Base1 {
public:
void display() const { cout << "Base1::display()" << endl; }
};
class Base2: public Base1 {
public:
void display() const { cout << "Base2::display()" << endl; }
};
class Derived: public Base2 {
public:
void display() const { cout << "Derived::display()" << endl; }
};
void fun(Base1 *ptr) {
ptr->display();
}
int main() {
Base1 base1;
Base2 base2;
Derived derived;
fun(&base1);
fun(&base2);
fun(&derived);
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class Base1 {
public:
Base1(int i) { cout << "Constructing Base1 " << i << endl; }
};
class Base2 {
public:
Base2(int j) { cout << "Constructing Base2 " << j << endl; }
};
class Base3 {
public:
Base3() { cout << "Constructing Base3 *" << endl; }
};
class Derived: public Base2, public Base1, public Base3 {
public:
Derived(int a, int b, int c, int d): Base1(a), member2(d), member1(c), Base2(b) { }
private:
Base1 member1;
Base2 member2;
Base3 member3;
};
int main() {
Derived obj(1, 2, 3, 4);
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class Base1 {
public:
Base1(int i) { cout << "Constructing Base1 " << i << endl; }
~Base1() { cout << "Destructing Base1" << endl; }
};
class Base2 {
public:
Base2(int j) { cout << "Constructing Base2 " << j << endl; }
~Base2() { cout << "Destructing Base2" << endl; }
};
class Base3 {
public:
Base3() { cout << "Constructing Base3 *" << endl; }
~Base3() { cout << "Destructing Base3" << endl; }
};
class Derived: public Base2, public Base1, public Base3 {
public:
Derived(int a, int b, int c, int d): Base1(a), member2(d), member1(c), Base2(b) { }
private:
Base1 member1;
Base2 member2;
Base3 member3;
};
int main() {
Derived obj(1, 2, 3, 4);
return 0;
}
#include <iostream>
using namespace std;
class Base1 {
public:
int var;
void fun() { cout << "Member of Base1" << endl; }
};
class Base2 {
public:
int var;
void fun() { cout << "Member of Base2" << endl; }
};
class Derived: public Base1, public Base2 {
public:
int var;
void fun() { cout << "Member of Derived" << endl; }
};
int main() {
Derived d;
Derived *p = &d;
d.var = 1;
d.fun();
d.Base1::var = 2;
d.Base1::fun();
p->Base2::var = 3;
p->Base2::fun();
return 0;
}
class A {
public:
void f();
};
class B {
public:
void f();
void g()
};
class C: public A, piblic B {
public:
void g();
void h();
};
#include <iostream>
using namespace std;
class Base0 {
public:
int var0;
void fun0() { cout << "Member of Base0" << endl; }
};
class Base1: public Base0 {
public:
int var1;
};
class Base2: public Base0 {
public:
int var2;
};
class Derived: public Base1, public Base2 {
public:
int var;
void fun() { cout << "Member of Derived" << endl; }
};
int main() {
Derived d;
d.Base1::var0 = 2;
d.Base1::fun0();
d.Base2::var0 = 3;
d.Base2::fun0();
return 0;
}
#include <iostream>
using namespace std;
class Base0 {
public:
int var0;
void fun0() { cout << "Member of Base0" << endl; }
};
class Base1: virtual public Base0 {
public:
int var1;
};
class Base2: virtual public Base0 {
public:
int var2;
};
class Derived: public Base1, public Base2 {
public:
int var;
void fun() { cout << "Member of Derived" << endl; }
};
int main() {
Derived d;
d.var0 = 2;
d.fun0();
return 0;
}
#include <iostream>
using namespace std;
class Base0 {
public:
Base0(int var) : var0(var) { }
int var0;
void fun0() { cout << "Member of Base0" << endl; }
};
class Base1: virtual public Base0 {
public:
Base1(int var) : Base0(var) { }
int var1;
};
class Base2: virtual public Base0 {
public:
Base2(int var) : Base0(var) { }
int var2;
};
class Derived: public Base1, public Base2 {
public:
Derived(int var) : Base0(var), Base1(var), Base2(var)
{ }
int var;
void fun()
{ cout << "Member of Derived" << endl; }
};
int main() {
Derived d(1);
d.var0 = 2;
d.fun0();
return 0;
}
#include <iostream>
#include "D:\Software\VSCode\C++\unit-7\7-10\account.h"
#include "D:\Software\VSCode\C++\account.cpp"
#include "D:\Software\VSCode\C++\date.cpp"
#include "D:\Software\VSCode\C++\accumulator.h"
#include "D:\Software\VSCode\C++\date.h"
using namespace std;
int main() {
Date date(2008, 11, 1);
SavingsAccount sa1(date, "S3755217", 0.015);
SavingsAccount sa2(date, "02342342", 0.015);
CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
sa1.deposit(Date(2008, 11, 5), 5000, "salary");
ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell");
sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323");
ca.settle(Date(2008, 12, 1));
ca.deposit(Date(2008, 12, 1), 2016, "repay the credit");
sa1.deposit(Date(2008, 12, 5), 5500, "salary");
sa1.settle(Date(2009, 1, 1));
sa2.settle(Date(2009, 1, 1));
ca.settle(Date(2009, 1, 1));
cout << endl;
sa1.show(); cout << endl;
sa2.show(); cout << endl;
ca.show(); cout << endl;
cout << "Total: " << Account::getTotal() << endl;
system("pause");
return 0;
}
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
#include "date.h"
#include "accumulator.h"
#include <string>
class Account {
private:
std::string id;
double balance;
static double total;
protected:
Account(const Date &date, const std::string &id);
void record(const Date &date, double amount, const std::string &desc);
void error(const std::string &msg) const;
public:
const std::string &getId() const { return id; }
double getBalance() const { return balance; }
static double getTotal() { return total; }
void show() const;
};
class SavingsAccount : public Account {
private:
Accumulator acc;
double rate;
public:
SavingsAccount(const Date &date, const std::string &id, double rate);
double getRate() const { return rate; }
void deposit(const Date &date, double amount, const std::string &desc);
void withdraw(const Date &date, double amount, const std::string &desc);
void settle(const Date &date);
};
class CreditAccount : public Account {
private:
Accumulator acc;
double credit;
double rate;
double fee;
double getDebt() const {
double balance = getBalance();
return (balance < 0 ? balance : 0);
}
public:
CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee);
double getCredit() const { return credit; }
double getRate() const { return rate; }
double getFee() const { return fee; }
double getAvailableCredit() const {
if (getBalance() < 0)
return credit + getBalance();
else
return credit;
}
void deposit(const Date &date, double amount, const std::string &desc);
void withdraw(const Date &date, double amount, const std::string &desc);
void settle(const Date &date);
void show() const;
};
#endif
#include "account.h"
#include <cmath>
#include <iostream>
using namespace std;
double Account::total = 0;
Account::Account(const Date &date, const string &id)
: id(id), balance(0) {
date.show();
cout << "\t#" << id << " created" << endl;
}
void Account::record(const Date &date, double amount, const string &desc) {
amount = floor(amount * 100 + 0.5) / 100;
balance += amount;
total += amount;
date.show();
cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
}
void Account::show() const {
cout << id << "\tBalance: " << balance;
}
void Account::error(const string &msg) const {
cout << "Error(#" << id << "): " << msg << endl;
}
SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate)
: Account(date, id), rate(rate), acc(date, 0) { }
void SavingsAccount::deposit(const Date &date, double amount, const string &desc) {
record(date, amount, desc);
acc.change(date, getBalance());
}
void SavingsAccount::withdraw(const Date &date, double amount, const string &desc) {
if (amount > getBalance()) {
error("not enough money");
} else {
record(date, -amount, desc);
acc.change(date, getBalance());
}
}
void SavingsAccount::settle(const Date &date) {
double interest = acc.getSum(date) * rate
/ date.distance(Date(date.getYear() - 1, 1, 1));
if (interest != 0)
record(date, interest, "interest");
acc.reset(date, getBalance());
}
CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee)
: Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) { }
void CreditAccount::deposit(const Date &date, double amount, const string &desc) {
record(date, amount, desc);
acc.change(date, getDebt());
}
void CreditAccount::withdraw(const Date &date, double amount, const string &desc) {
if (amount - getBalance() > credit) {
error("not enough credit");
} else {
record(date, -amount, desc);
acc.change(date, getDebt());
}
}
void CreditAccount::settle(const Date &date) {
double interest = acc.getSum(date) * rate;
if (interest != 0)
record(date, interest, "interest");
if (date.getMonth() == 1)
record(date, -fee, "annual fee");
acc.reset(date, getDebt());
}
void CreditAccount::show() const {
Account::show();
cout << "\tAvailable credit:" << getAvailableCredit();
}
#ifndef __ACCUMULATOR_H__
#define __ACCUMULATOR_H__
#include "date.h"
class Accumulator {
private:
Date lastDate;
double value;
double sum;
public:
Accumulator(const Date &date, double value)
: lastDate(date), value(value), sum(0) { }
double getSum(const Date &date) const {
return sum + value * date.distance(lastDate);
}
void change(const Date &date, double value) {
sum = getSum(date);
lastDate = date;
this->value = value;
}
void reset(const Date &date, double value) {
lastDate = date;
this->value = value;
sum = 0;
}
};
#endif
#ifndef __DATE_H__
#define __DATE_H__
class Date {
private:
int year;
int month;
int day;
int totalDays;
public:
Date(int year, int month, int day);
int getYear() const { return year; }
int getMonth() const { return month; }
int getDay() const { return day; }
int getMaxDay() const;
bool isLeapYear() const {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
void show() const;
int distance(const Date& date) const {
return totalDays - date.totalDays;
}
};
#endif
#include "date.h"
#include <iostream>
#include <cstdlib>
using namespace std;
namespace {
const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
}
Date::Date(int year, int month, int day) : year(year), month(month), day(day) {
if (day <= 0 || day > getMaxDay()) {
cout << "Invalid date: ";
show();
cout << endl;
exit(1);
}
int years = year - 1;
totalDays = years * 365 + years / 4 - years / 100 + years / 400
+ DAYS_BEFORE_MONTH[month - 1] + day;
if (isLeapYear() && month > 2) totalDays++;
}
int Date::getMaxDay() const {
if (isLeapYear() && month == 2)
return 29;
else
return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
}
void Date::show() const {
cout << getYear() << "-" << getMonth() << "-" << getDay();
}