单继承
#include<iostream>
#include<cstring>
using namespace std;
class Base
{
private:
int n;
public:
Base(int a)
{
n = a;
}
void show()
{
cout << "n=" << n << endl;
}
};
class A :public Base
{
private:
int m;
public:
A(int a, int b);//请实现该构造函数
void showa()
{
show();
cout << "m=" << m << endl;
}
};
A::A(int a, int b) :Base(a)
{
m = b;
}
int main()
{
A dobj(10, 20);
dobj.showa();
return 0;
}
单继承-子对象
#include<iostream>
using namespace std;
class Base
{
private:
int n;
public:
Base(int a)
{
n = a;
cout << "n=" << n << endl;
}
};
class A :public Base
{
private:
int m;
Base obj;
public:
A(int a, int b, int c);//请实现该构造函数
};
A::A(int a, int b, int c) :Base(a), obj(c)
{
m = b;
cout << "m=" << m<< endl;
}
int main()
{
A dobj(10, 20, 30);
return 0;
}
虚基类
#include<iostream>
using namespace std;
class Base
{
protected:
int a;
public:
Base(int x) :a(x)//a=x
{
}
};
class Base1 : virtual public Base
{
protected:
int b;
public:
Base1(int x, int y) : Base(y), b(x)//y=a,b=x
{
}
};
class Base2 : virtual public Base
{
protected:
int c;
public:
Base2(int x, int y) : Base(y)//y=a,c=x
{
c = x;
}
};
class Derived : public Base1, public Base2
{
protected:
int d3;
public:
Derived(int x, int y); //请实现该函数
void show()
{
cout << "a=" << a << endl;
cout << "Base::a=" << Base::a << endl;
cout << "Base1::a=" << Base1::a << endl;
cout << "Base2::a=" << Base2::a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
}
};
Derived::Derived(int x, int y) :Base1(30,b), Base2(30,c), Base(30)
{
b = x;
c = y;
}
int main()
{
Derived dobj(10, 20);
dobj.show();
return 0;
}
实现一个Singer类,通过以下测试:
#include<iostream>
using namespace std;
class Singer
{
private:
string name;
string sexual;
int age;
double mark;
public:
Singer(string n="null", string s="NULL", int a=0, double m=0);
string getName();
friend istream& operator>>(istream &is, Singer& s);
friend ostream& operator<<(ostream &is, const Singer& s);
friend bool operator==(const Singer &s1, const Singer& s2);
friend bool operator>(const Singer &s1, const Singer& s2);
};
Singer::Singer(string n, string s, int a, double m)
{
name = n;
sexual = s;
age = a;
mark = m;
}
string Singer::getName()
{
return name;
}
istream &operator>>(istream &is, Singer &s)
{
is >> s.name >> s.sexual >> s.age >> s.mark;
return is;
}
ostream &operator<<(ostream& is, const Singer &s)
{
is << s.name <<" "<< s.sexual <<" "<< s.age <<" "<< s.mark;
return is;
}
bool operator==(const Singer& s1, const Singer& s2)
{
if (s1.mark == s2.mark)
return true;
else
return false;
}
bool operator>(const Singer& s1, const Singer& s2)
{
if (s1.mark > s2.mark)
return true;
else
return false;
}
int main()
{
Singer s1,s2;
cin>>s1>>s2;
cout<<s1<<"\n"<<s2<<endl;
if(s1>s2)
cout<<s1.getName()<<"'s score is higher than "<<s2.getName()<<"'s.\n";
else if(s1==s2)
cout<<s1.getName()<<"'s score is equal to "<<s2.getName()<<"'s.\n";
else
cout<<s1.getName()<<"'s score is lower than "<<s2.getName()<<"'s.\n";
return 0;
}
Sales_data类
#include<iostream>
using namespace std;
class Sales_data {
//依次输入书号、销量和收入
friend istream& operator>>(istream&, Sales_data&);
//依次输出书号、销量、收入和均价
friend ostream& operator<<(ostream&, const Sales_data&);
friend bool operator==(const Sales_data&, const Sales_data&);
friend bool operator!=(const Sales_data&, const Sales_data&);
// for "+", assume that both objects refer to the same book
friend Sales_data operator+(const Sales_data&, const Sales_data&);
public:
Sales_data() : units_sold(0), revenue(0.0) {}
Sales_data(const string& s, unsigned n, double r) : bookNo(s), units_sold(n), revenue(r) {}
string get_bookNo() const
{
return bookNo;
};
// for "+=", assume that both objects refer to the same book
Sales_data& operator+=(const Sales_data &s)
{
this->units_sold = this->units_sold + s.units_sold;
this->revenue = this->revenue + s.revenue;
return *this;
};
private:
double avg_price() const
{
return revenue / units_sold;
};//均价,等于收入除以销量
string bookNo; //书号
unsigned units_sold; //销量
double revenue; //收入
};
istream & operator>>(istream &is, Sales_data &s)
{
is >> s.bookNo >> s.units_sold >> s.revenue;
return is;
}
ostream& operator<<(ostream& is, const Sales_data& s)
{
is << s.bookNo <<" "<< s.units_sold <<" "<< s.revenue <<" "<< s.avg_price();
return is;
}
bool operator==(const Sales_data& s1, const Sales_data& s2)
{
if (s1.units_sold == s2.units_sold)
return true;
else
return false;
}
bool operator!=(const Sales_data& s1, const Sales_data& s2)
{
if (s1.units_sold != s2.units_sold)
return true;
else
return false;
}
Sales_data operator+(const Sales_data &s1, const Sales_data &s2)
{
Sales_data s3;
s3.bookNo = s1.bookNo;
s3.units_sold = s1.units_sold + s2.units_sold;
s3.revenue = s1.revenue + s2.revenue;
return s3;
}
int main() {
Sales_data item1, item2;
while (cin >> item1 >> item2) {
cout << item1 << "\n" << item2 << "\n";
if (item1 == item2)
cout << item1.get_bookNo() << " equals " << item2.get_bookNo() << "\n";
if (item1 != item2)
cout << item1.get_bookNo() << " doesn't equal " << item2.get_bookNo() << "\n";
cout << (item1 + item2) << "\n";
item1 += item2;
cout << item1 << "\n";
}
return 0;
}
Complex类
#include<iostream>
using namespace std;
class Complex
{
private:
double x;
double y;
public:
Complex(double x = 0.0, double y = 0.0)
{
};
Complex& operator+=(const Complex& c)
{
this->x = this->x + c.x;
this->y = this->y + c.y;
return *this;
};
Complex& operator-=(const Complex &c)
{
this->x = this->x - c.x;
this->y = this->y - c.y;
return *this;
};
Complex& operator*=(const Complex& c)
{
*this = *this * c;
return *this;
};
Complex& operator/=(const Complex& c)
{
*this = *this / c;
return *this;
};
friend Complex operator+(const Complex&, const Complex&);
friend Complex operator-(const Complex&, const Complex&);
friend Complex operator*(const Complex&, const Complex&);
friend Complex operator/(const Complex&, const Complex&);
friend bool operator==(const Complex&, const Complex&);
friend bool operator!=(const Complex&, const Complex&);
friend ostream& operator<<(ostream&, const Complex&);
friend istream& operator>>(istream&, Complex&);
};
ostream& operator<<(ostream& is, const Complex& c)
{
is << c.x <<" " << "+" <<" "<< c.y << "i";
return is;
}
istream& operator>>(istream& is, Complex& c)
{
is >> c.x >> c.y;
return is;
}
Complex operator+(const Complex &c1, const Complex &c2)
{
Complex c3;
c3.x = c1.x + c2.x;
c3.y = c1.y + c2.y;
return c3;
}
Complex operator-(const Complex& c1, const Complex& c2)
{
Complex c3;
c3.x = c1.x - c2.x;
c3.y = c1.y - c2.y;
return c3;
}
Complex operator*(const Complex& c1, const Complex& c2)
{
Complex c3;
c3.x = c1.x * c2.x - c1.y * c2.y;
c3.y = c1.x * c2.y + c1.y * c2.x;
return c3;
}
Complex operator/(const Complex& c1, const Complex& c2)
{
Complex c3;
c3.x = (c1.x * c2.x + c1.y * c2.y) / (c2.x * c2.x + c2.y * c2.y);
c3.y = (c1.y * c2.x - c1.x * c2.y) / (c2.x * c2.x + c2.y * c2.y);
return c3;
}
boo
评论0