设计一个类,使它具有一个计算两个数之和的成员函数。并且写出它的构造函数和析构函数。并使用一个测试程序来测试这个类的执行,观察各个函数之间的调用顺序。
#include<iostream>
using namespace std;
class number
{
public:
number(int a,int b);
int add();
~number(){}
private:
int number1,number2,sum;
};
number::number(int a,int b)
{
number1=a;
number2=b;
sum=a+b;
}
int number::add()
{
return sum;
}
int main()
{
int number1,number2;
cout<<"输入";
cin>>number1>>number2;
number mynumber(number1,number2);
int sum=mynumber.add();
cout<<number1<<"+"<<number2<<"="<<sum;
return 0;
}
设计一个用于人事管理的People(人员)类,考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。其中“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、组合。
#include "pch.h"
#include <iostream>
#include<string>
using namespace std;
class Date
{
public:
Date(void) { }
~Date(void) { }
int year, month, day;
};
class People
{
public:
People(int num, string se, int y, int m, int d, long long i);
~People(void);
void setnumber(int num);
void setsex(string se);
void setbirthday(int y, int m, int d);
void setid(long long i);
int getnumber(void);
string getsex(void);
Date getbirthday(void);
long long getid(void);
private:
int number;
string sex;
Date birthday;
long long id;
};
People::People(int num, string se, int y, int m, int d, long long i)
{
number = num; sex = se; birthday.year = y; birthday.month = m; birthday.day = d; id = i;
}
People::~People(void)
{ }
void People::setnumber(int num)
{
number = num;
}
void People::setsex(string se)
{
sex = se;
}
void People::setbirthday(int y, int m, int d)
{
birthday.year = y; birthday.month = m; birthday.day = d;
}
void People::setid(long long i)
{
id = i;
}
int People::getnumber(void)
{
return number;
}
string People::getsex(void)
{
return sex;
}
Date People::getbirthday(void)
{
return birthday;
}
long long People::getid(void)
{
return id;
}
int main(void)
{
People myPeople(0, " ", 0000, 00, 00, 000000000000000000);
cout << "以此输入 编号,性别,出生日期,身份证号" << endl;
int n;
cin >> n;
myPeople.setnumber(n);
string se;
cin >> se;
myPeople.setsex(se);
int y, m, d;
cin >> y >> m >> d;
myPeople.setbirthday(y, m, d);
long long i;
cin >> i;
myPeople.setid(i);
cout << "number : " << myPeople.getnumber() << endl << "sex : " << myPeople.getsex() << endl;
cout << "birthday : " << myPeople.getbirthday().year << "year ";
cout << myPeople.getbirthday().month << "month " << myPeople.getbirthday().day << "day " << endl << "id : " << myPeople.getid() << endl;
return 0;
}
定义一个圆类(Circle),属性为半径(radius)、圆周长和面积,操作为输入半径并计算周长、面积,输出半径、周长和面积。要求定义构造函数(以半径为参数,缺省值为0)和复制构造函数。
#include <iostream>
using namespace std;
const double Pi = 3.14159265358979;
class Circle
{
public:
Circle ( double r ) { radius = r; }
Circle ( void ) { radius = 0; }
Circle ( Circle &p ) { radius = p.radius; }
~Circle ( ) { }
void calculate ( double r ) { length = 2 * Pi * radius; area = Pi * radius * radius; }
double getradius ( void ) { return radius; }
double getlength ( void ) { return length; }
double getarea ( void ) { return area; }
private:
double radius;
double length;
double area;
};
int main ( void )
{
Circle myCircle1 ( 1.0 );
Circle myCircle2 ( myCircle1 );
cout << myCircle2.getradius () << endl;
}
创建一个employee类,该类中用字符数组表示姓名、街道地址、市、省和邮政编码。在类中定义构造函数、changename()、display()函数,构造函数用来初始化每个成员, display()函数把完整的对象数据打印出来。其中数据成员是保护的,函数是公共的。
#include "pch.h"
#include <iostream>
#include<string>
#include <cstring>
using namespace std;
class employee
{
public:
employee(const char *n, const char *j, const char *s, const char *e, const char *y);
~employee();
void changename(const char *n, const char *j, const char *s, const char *e, const char *y);
void display();
private:
char name[10];
char jiedao[10];
char shi[10];
char sheng[10];
char youzheng[10];
};
employee::employee(const char *n, const char *j, const char *s, const char *e, const char *y)
{
strcpy_s(name, n);
strcpy_s(jiedao, j);
strcpy_s(shi, s);
strcpy_s(sheng, e);
strcpy_s(youzheng, y);
}
void employee::changename(const char *n, const char *j, const char *s, const char *e, const char *y)
{
strcpy_s(name, n);
strcpy_s(jiedao, j);
strcpy_s(shi, s);
strcpy_s(sheng, e);
strcpy_s(youzheng, y);
}
void employee::display()
{
cout << name << jiedao << shi << sheng << youzheng << endl;
}
employee::~employee()
{
}
int main()
{
employee myemployee("name", " jiedao", " shi", " sheng", "00000");
myemployee.changename("name", " jiedao", " shi", " sheng", "00000");
myemployee.display();
}
编写一个程序,设计一个类Trig,给定三角形的三边长x、y、z,包含一个友元函数计算两个三角形面积之和。
#include "pch.h"
#include <iostream>
#include <cmath>
using namespace std;
class tring
{
public:
tring(float xx, float yy, float zz);
tring(tring &t);
float getx()
{
return x;
}
float gety()
{
return y;
}
float getz()
{
return z;
}
~tring();
friend double sum(tring &t1, tring &t2);
private:
float x,y,z,p,area;
};
tring::tring(float xx, float yy, float zz)
{
x = xx;
y = yy;
z = zz;
p = (x + y + z) / 2;
area = sqrt(p * (p - x) * (p - y) * (p - z));
}
tring::tring(tring&t)
{
x = t.x;
y = t.y;
z = t.z;
p = (x + y + z) / 2;
area = sqrt(p * (p - x) * (p - y) * (p - z));
}
double sum(tring&t1, tring&t2)
{
return(t1.area + t2.area);
}
tring:: ~tring()
{
void;
}
int main()
{
float x1, x2, y1, y2, z1, z2;
cout << "输入第一个三角形三个边长" << endl;
cin >> x1 >> y1 >> z1;
cout << "输入第二个三角形三个边长" << endl;
cin >> x2 >> y2 >> z2;
tring tr1(x1, y1, z1);
tring tr2(x2, y2, z2);
cout << "两个三角形面积和为" << endl;
cout << sum(tr1, tr2) << endl;
return 0;
}
定义类X,Y,Z,函数h(X),满足:类X有私有成员i,Y的成员函数g(X)是X的友元函数,实现对X的成员i加1;类Z是类X的友元类,其成员函数f (X)实现对X的的成员i加5;函数h(X)是X的友元函数,实现对X的成员i加10。
#include <iostream>
using namespace std;
class x;
class y
{
public:
void g(x*p);
};
class x
{
public:
x(int ii){i=ii;}
~x(void){}
int show(void){return i;}
friend void y::g(x*);
friend void h(x*);
private:
int i;
};
void y::g(x*p)
{
p->i++;
}
class z
{
public:
void f(x*p)
{
p->i+=5;
}
};
void h(x*p)
{
p->i+=10;
}
int main (void)
{
x x(0);
y y;
z z;
y.g(&x);
z.f(&x);
h(&x);
cout<<x.show()<<endl;
return 0;
}
声明一个基类Animal,有私有整型成员变量age,构造其派生类dog,在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把age改成公有成员变量,还会有问题吗?编程试试看。
#include "pch.h"
#include <iostream>
using namespace std;
class Animal
{
public:
int age;
};
class dog : public Animal
{
public:
void setAge(int n)
{
age = n;
}
};
int main()
{
dog dogg;
dogg.setAge(1);
return 0;
}
定义一个基类BaseClass,从它派生出类DerivedClass,BaseClass有成员函数fn1()、fn2(),DerivedClass也有成员函数fn1()、fn2(),在主程序中定义一个DerivedClass的对象,分别用DerivedClass的对象以及BaseClass和DerivedClass的指针来调用fn1()、fn2(),观察运行结果。
#include "pch.h"
#include <iostream>
using namespace std;
class BaseClass
{
public:
void fn1(void) { cout << "fn1" << endl; }
void fn2(void) { cout << "fn2" << endl; }
};
class DerivedClass : public BaseClass
{
public:
void fn1(void) { cout << "fn1.1" << endl; }
void fn2(void) { cout << "fn2.2" << endl; }
};
int main(void)
{
BaseClass zhangmuniang;
BaseClass* bp = &zhangmuniang;
DerivedClass nvpengyou;
DerivedClass* dp = &nvpengyou;
nvpengyou.fn1();
nvpengyou.fn2();
bp->fn1();
bp->fn2();
dp->fn1();
dp->fn2();
return 0;
}
声明一个基类BaseClass,有整型成员变量Number,构造其派生类DerivedClass,观察构造函数和析构函数的执行情况。
#include "pch.h"
#include <iostream>
using namespace std;
class BaseClass
{
public:
BaseClass(int n) { Number = n; }
~BaseClass(void) { }
int Number;
};
class DerivedClass : public BaseClass
{
public:
DerivedClass(int n) : BaseClass(n) { }
~DerivedClass(void) { }
};
int main(void)
{
DerivedClass dderivedclass(1814010130);
cout << dderivedclass.Number << endl;
return 0;
}
声明一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。自行车类有高度(Height)等属性,汽车类有座位数(SeatNum)等属性。从bicycle和motorcar派生出摩托车(motorcycle)类,在继承过程中,注意把vehicle设置成虚基类。如果不把vehicle设置成虚基类,会有什么问题?编程试试看。
#include "pch.h"
#include <iostream>
using namespace std;
class Vehicle
{
public:
virtual void Run() { cout << "Vehicle::Run" << endl; }
virtual void Stop() { cout << "Vehicle::Stop"<<endl; }
int MaxSpeed, Weight;
};
class motorcar :virtual public Vehicle
{
public:
int SeatNum;
void Run() { cout << "motorcar:Run" << endl; }
virtual void Stop() { cout << "motorcar:Stop" << endl; }
};
class bicycle :virtual public Vehicle
{
public:
int Height;
virtual void Run() { cout << "bicycle:Run" << endl; }
void Stop() { cout << "bicycle:Stop" << endl; }
};
class motorcycle :public bicycle, public motorcar
{
public:
void Run() { cout << "motorcycle:Run" << endl; }
void Stop() { cout << "mototrcycle:Stop" << endl; }
};
int main(void)
{
motorcycle mymotorcycle;
mymotorcycle.Run();
mymotorcycle.Stop();
cout << mymotorcycle.MaxSpeed << endl;
cout << mymotorcycle.Weight << endl;
Vehicle myVehicle;
myVehicle.Run();
myVehicle.Stop();
cout << myVehicle.MaxSpeed << endl;
cout << myVehicle.Weight << endl;
bicycle mybicycle;
mybicycle.Run();
mybicycle.Stop();
cout << mybicycle.MaxSpeed << endl;
cout << mybicycle.Weight << endl;
cout << mybicycle.Height << endl;
motorcar mymotorcar;
mymotorcar.Run();
mymotorcar.Stop();
cout << mymotorcar.MaxSpeed << endl;
cout << mymotorcar.Weight << endl;
cout << mymotorcar.SeatNum << endl;
return 0;
}
声明一个复数类,用来定义复数,并且重载运算符“+”、“-”,用来完成复数的加法和减法操作,其中“+”重载成成员函数,“-”重载成成员函数。并且通过复数类来实现两个复数5+3i与2+2i的和与差。
#include "pch.h"
#include <iostream>
using namespace std;
class AA
{
public:
AA() { cout << "constructorAA 1" << endl; }
~AA() { cout << "destructorAA 2" << endl; }
};
class BB
{
public:
BB() { cout << "constructorBB 1" << endl; }
~BB() { cout << "destructorBB 2" << endl; }
private:
AA a;
};
void main()
{
AA a; BB b;
};
声明Point类,有坐标x,y两个成员变量;对Point类重载前置单目“++”(自增)、后置单目“--”(自减)运算符,实现对坐标值的改变。
#include "pch.h"
#include<cstdio>
#include<iostream>
using namespace std;
class Point
{
public:
Point(int _x= 0, int _y = 0):x(_x),y(_y){}
~Point(){}
Point operator ++();
Point operator--(int);
void display() { cout <<"("<< x << "," << y << ")"<<endl; }
private:
int x;
int y;
};
Point Point::operator ++()
{
Point po;
po.x = x + 1;
po.y = y + 1;
x += 1;
y += 1;
return po;
}
Point Point::operator--(int)
{
Point po;
po.x = x;
po.y = y;
x -= 1;
y -= 1;
return po;
}
int main()
{
Point po(0,0),po1(1, 2), po2(3, 4);
po = ++po1;
po1.display();
po = po2--;
po2.display();
return 0;
}
声明一个车(vehicle)类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车类(motorcar)类,从bicycle类和motorcar类派生出摩托车类(motorcycle)类,他们都有Run、Stop等成员函数。观察虚函数的作用。
#include "pch.h"
#include<iostream>
using namespace std;
class Vehicle
{
public:
virtual void Run() { cout << "Vehicle::Run called\n"; }
virtual void Stop() { cout << "Vehicle::Stop called\n"; }
int MaxSpeed, Weight;
};
class motorcar :virtual public Vehicle
{
public:
int SeatNum;
void Run() { cout << "motorcar:Run" << endl; }
virtual void Stop() { cout << "motorcar:Stop" << endl; }
};
class bicycle :virtual public Vehicle
{
public:
int Height;
virtual void Run() { cout << "bicycle:Run" << endl; }
void Stop() { cout << "bicycle:Stop"<<endl; }
};
class motorcycle :public bicycle, public motorcar
{
public:
void Run() { cout << "motorcycle:Run" << endl; }
void Stop() { cout << "mototrcycle:Stop" << endl; }
};
int main()
{
Vehicle *mycar;
Vehicle ve;
mycar = &ve;
mycar->Run();
mycar->Stop();
bicycle bi;
mycar = &bi;
mycar->Run();
mycar->Stop();
motorcar mo;
mycar = &mo;
mycar->Run();
mycar->Stop();
motorcycle mocy;
mycar = &mocy;
mycar->Run();
mycar->Stop();
}
编写一个哺乳动物类Mammal,再由此基础上派生出狗类Dog,二者都声明speak()成员函数,该函数在基类中被声明为虚函数。声明一个Dog类的对象,通过此对象调用speak函数,观察运行结果。
#include "pch.h"
#include<cstdio>
#include<iostream>
using namespace std;
class Mammal
{
public:
Mammal() {}
virtual void speak() { cout << "Mammal speak." << endl; }
};
class Dog : public Mammal
{
public:
Dog() {}
void speak() { cout << "Dog speak." << endl; }
};
int main()
{
Dog mydog;
mydog.speak();
Mammal myMammal;
myMammal.speak();
return 0;
}