实验3.2
#include <iostream>
using namespace std;
enum CPU_rank { P1 = 1, P2, P3, P4, P5, P6, P7 };
class CPU
{
private:
CPU_rank rank;
int frequency;
float voltage;
public:
CPU(CPU_rank r, int f, float v)
{
rank = r;
frequency = f;
voltage = v;
cout << "构造了一个CPU!" << endl;
}
CPU() { cout << "构造了一个CPU!" << endl; };
~CPU() { cout << "析构了一个CPU!" << endl; }
void Run() { cout << "CPU开始运行!" << endl; }
void Stop() { cout << "CPU停止运行!" << endl; }
};
class RAM
{
public:
RAM() { cout << "构造了一个RAM!" << endl; }
~RAM() { cout << "析构了一个RAM!" << endl; }
void Run() { cout << "RAM开始运行!" << endl; }
void Stop() { cout << "RAM停止运行!" << endl; }
};
class CDROM
{
public:
CDROM() { cout << "构造了一个CDROM!" << endl; }
~CDROM() { cout << "析构了一个CDROM!" << endl; }
void Run() { cout << "CDROM开始运行!" << endl; }
void Stop() { cout << "CDROM停止运行!" << endl; }
};
class computer
{
private:
CPU cpu;
RAM ram;
CDROM cdrom;
public:
computer():cpu(CPU(P3,10,20))
{
cout << "构造了一个computer!" << endl;
}
~computer() { cout << "析构了一个computer!" << endl; }
void Run()
{
cout << "computer开始运行!" << endl;
cpu.Run();
ram.Run();
}
void Stop()
{
ram.Stop();
cpu.Stop();
cout << "computer停止运行!" << endl;
}
};
int main()
{
computer a;
a.Run();
a.Stop();
return 0;
}
实验3.3
#include<iostream>
#include<string>
using namespace std;
class date {
public:`在这里插入代码片`
date(int x = 0, int y = 0, int z = 0)
{
year = x; month = y; day = z;
}
date(date& d);
void setdate();
void showdate();
private:
int year, month, day;
};
date::date(date& d) {
year = d.year;
month = d.month;
day = d.day;
}
void date::setdate() {
int a, b, c; cout << "输入日期 ";
cin >> a >> b >> c;
year = a;
month = b;
day = c;
}
void date::showdate() { cout << year << " 年" << month << " 月" << day << " 日" << endl; }
class people {
public:
people(date bd, long int nb = 0, long int idnb = 0, string sx = "男");
people(people& p);
void setpeople();
void showpeople();
private:
int number, idnumber;
string sex;
date birthday;
};
people::people(date bd, long int nb, long int idnb, string sx) :birthday(bd)
{
birthday = bd;
number = nb;
idnumber = idnb;
sex = sx;
}
people::people(people& p) :birthday(p.birthday)
{
number = p.number;
birthday = p.birthday;
sex = p.sex;
idnumber = p.idnumber;
}
void people::setpeople()
{
date a;
long int b, c;
char d;
a.setdate(); cout << "号码 :";
cin >> b;
cout << "id:";
cin >> c;
cout << "性别 :";
cin >> d;
cout << endl;
birthday = a;
number = b;
idnumber = c;
sex = d;
}
void people::showpeople()
{
cout << "number: " << number << endl;
cout << "idnumber: " << idnumber << endl;
cout << "sex: " << sex << endl;
cout << "birthday: ";
birthday.showdate();
}
int main()
{
date b;
people p1(b);
p1.setpeople();
people p2(p1);
p1.showpeople();
return 0;
}
实验4.1
#include<iostream>
using namespace std;
class Animal
{
public:
int age;
};
class dog :public Animal
{
public:
void SetAge(int n) { age = n; }
int GetAge() { return age; }
};
int main()
{
dog d;
d.SetAge(10);
cout << "The age of dog is " << d.GetAge() << endl;
return 0;
}
实验4.2
#include<iostream>
using namespace std;
class BaseClass
{
public:
BaseClass(int a=0) :m_a(a)
{
cout << "构造了BaseClass类的对象" << endl;
//cout << "m_a = " << m_a << endl;
}
~BaseClass() { cout << "析构了BaseClass类的对象" << endl; }
private:
int m_a;
};
class DerivedClass :public BaseClass
{
public:
DerivedClass(int b=0):m_b(b)
{
cout << "构造了DerivedClass类的对象" << endl;
//cout << "m_b = " << m_b << endl;
}
~DerivedClass() { cout << "析构了DerivedClass的对象" << endl; }
private:
int m_b;
};
int main()
{
DerivedClass d;
return 0;
}
实验4.3
#include<iostream>
using namespace std;
class vehicle
{
public:
void Run() { cout << "车开始运行" << endl; }
void Stop() { cout << "车停止运行" << endl; }
protected:
int MaxSpeed;
int Weight;
};
class bicycle :virtual public vehicle
{
protected:
int Height;
};
class motorcar :virtual public vehicle
{
protected:
int SeatNum;
};
class motorcycle :public bicycle, public motorcar
{
public:
motorcycle(int m, int w, int h, int s)
{
MaxSpeed = m, Weight = w, Height = h, SeatNum = s;
}
void show()
{
cout << "MaxSpeed: " << MaxSpeed << "\nWeight: " << Weight
<< "\nHeight: " << Height << "\nSeatNum:" << SeatNum << endl;
}
};
int main()
{
motorcycle mo(100, 150, 130, 2);
mo.Run();
mo.show();
mo.Stop();
return 0;
}
实验5.3
student.h
#include "Person.h"
#ifndef _Student_H_
#define _Student_H_
class Student:public Person
{
public:
Student();
Student(string name, int age, int sex, string id);
void resetStudent(string name, int age, int sex, string id);
void showStudent();
private:
string m_id;
};
#endif
student.cpp
#include <iostream>
#include "Student.h"
Student::Student() :Person()
{
m_id = 1433223;
}
Student::Student(string name, int age, int sex, string id) :Person(name, age, sex)
{
m_id = id;
}
void Student::resetStudent(string name, int age, int sex, string id)
{
resetPerson(name, age, sex);
m_id = id;
}
void Student::showStudent()
{
showPerson();
cout <<"学号 " << m_id << endl;
}
person.h
#ifndef _Person_H_
#define _Person_H_
#include <string>
using namespace std;
class Person
{
public:
Person();
Person(string name, int age, int sex);
void resetPerson(string name, int age, int sex);
void showPerson();
private:
string m_Name;
int m_Age;
int m_Sex;//1为男 2为女
};
#endif // !_Person_H_
person.cpp
#include <iostream>
#include "Person.h"
using namespace std;
Person::Person()
{
m_Name = "张灿";
m_Age = 18;
m_Sex = 1;
}
Person::Person(string name, int age, int sex)
{
m_Name = name;
m_Age = age;
m_Sex = sex;
}
void Person::resetPerson(string name, int age, int sex)
{
m_Name = name;
m_Age = age;
m_Sex = sex;
}
void Person::showPerson()
{
cout << "姓名 "<<m_Name << endl << "年龄 " << m_Age << endl
<< "性别 " << m_Sex << "(1为男2为女)" << endl;
}
运算符重载
递增运算符重载
#include <iostream>//algorithm,bits/stdc++.h
using namespace std;
//int a = 10;
//cout << ++a << endl;//11
//cout << a << endl;//11
//int b = 10;
//cout << b++ << endl;//10
//cout << b << endl;//11
//递增运算符重载 ++
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
MyInteger()
{
m_Num = 0;
}
//重载前置++运算符
MyInteger& operator++()
{
m_Num++;
return *this;
}
//重载后置++运算符
MyInteger operator++(int) //int代表占位参数,可以区分前置和后置
{
//先记录当时结果
MyInteger temp = *this;
//后递增
m_Num++;
//最后将记录结果做返回
return temp;
}
//重载前置--运算符
MyInteger& operator--()
{
//先运算
this->m_Num--;
//后返回
return *this;
}
//重载后置--运算符
MyInteger operator--(int)
{
//先储存
MyInteger temp = *this;
//后递减
m_Num--;
//最后将储存结果返回
return temp;
}
private:
int m_Num;
};
//重载左移运算符
ostream& operator<<(ostream& cout,MyInteger myint)
{
cout << myint.m_Num;
return cout;
}
void test01()
{
MyInteger myint;
cout << ++myint << endl;
}
void test02()
{
MyInteger myint;
cout << myint << endl;
cout << myint++ << endl;
cout << myint << endl;
}
int main()
{
return 0;
}
左移运算符
#include <iostream>
using namespace std;
class Person
{
public:
Person(int a=0,int b=0):m_A(a),m_B(b){}
int m_A;
int m_B;
};
//重载左移运算符
ostream& operator<<(ostream& cout, Person& p)
{
cout << p.m_A << " " << p.m_B;
return cout;
}
void test01()
{
Person p(10,10);
cout << p;
}
int main()
{
test01();
return 0;
}
加号
#include <iostream>
using namespace std;
//加号运算符重载
//
class Price
{
/*friend Price operator+(Price& p1, Price& p2);*/
public:
Price(double _dollars=0,double _cents=0):dollars(_dollars),cents(_cents){}
Price operator+(Price& p);//成员函数重载+号(声明)
void show_data();
private:
double dollars;
double cents;
};
void Price::show_data()
{
cout <<"your account is " << this->dollars<<" dollars "
<< this->cents<<" cents";
}
//成员函数重载+号(定义)
Price Price::operator+(Price& p)
{
Price temp;
temp.dollars = this->dollars + p.dollars;
temp.cents = this->cents + p.cents;
return temp;
}
//全局函数重载+号
//Price operator+(Price& p1, Price& p2)
//{
// Price temp;
// temp.dollars = p1.dollars + p2.dollars;
// temp.cents = p1.cents + p2.cents;
//
// return temp;
//}
void test()
{
Price p1(100, 5.2);
Price p2(0, 6.4);
Price p3 = p1 + p2;//本质:Person p3 = p1.operator+ (p2);(成员函数重载的本质)
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
p3.show_data();
}
int main()
{
test();
return 0;
}