3.14 C++上机
构造函数与构析函数,复制构造函数,内敛成员函数,带默认形参值的成员函数,类的组合
#include <bits/stdc++.h>
#define cl(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1e5+50;
class date
{
public:
int year,mon,day;
//date(int y,int m,int d):year(y),mon(m),day(d){}
};
class stuff
{
private:
int num;
int sex;
date born;
string idnum;
public:
stuff(int num1,int sex1,string idnum1,date born1)
{
num=num1;
idnum.clear();
idnum+=idnum1;
born.day=born1.day;
sex=sex1;
born.mon=born1.mon;
born.year=born1.year;
}
stuff (stuff &a);
void display()
{
printf("No.:%d\n",num);
printf("sex:%d\n",sex);
printf("id number: ");
cout<<idnum<<endl;
printf("born date:\ %d-%d-%d\n",born.year,born.mon,born.day);
}
~stuff(){};
int f(int num,int sex=0)
{
cout<<num<< " "<<sex<<endl;
}
};
inline stuff::stuff(stuff &a)
{
num=a.num;
idnum+=a.idnum;
born.day=a.born.day;
born.mon=a.born.mon;
born.year=a.born.year;
sex=a.sex;
printf("COPY CONSTRUCTOR\n");
}
int main()
{
date d;
d.year=2018;
d.mon=3;
d.day=14;
stuff a(1,1,"110120119123456",d);
a.display();
stuff b(a);
b.display();
return 0;
}
二、
定义一个复数类Complex,使得下面的代码能够工作
Complex c1(3,5); //用复数3+5i初始化c1
Complex c2=4.5; //用实数4.5初始化c2
c1.add(c2); //将c1与c2相加,结果保存在c1中
c1.show(); //将c1输出(这时的结果应该是7.5+5i)
#include <bits/stdc++.h>
#define cl(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1e5+50;
class Complex
{
private:
double x;
int i;
public:
Complex(double x1,int y1)
{
x=x1;
i=y1;
}
Complex(double x1);
void add(Complex c)
{
x+=c.x;
i+=c.i;
}
void show()
{
printf("%lf + %di",x,i);
}
};
Complex::Complex(double x1)
{
x=x1;
i=0;
}
int main()
{
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
return 0;
}
- 设计CPU类
//class header
enum CPU_Rank {P1=1,P2,P3,P4,P5,P6,P7};
class CPU
{
CPU_Rank rank;
int frequency;
float voltage;
//constructor for initialized instance varibles and display “construct a object”
//Destructor for display “destructure a object”;
//Get and set functions for each instance varible of the class
//Run() to display “CPU is runing”;
// Stop() to display CPU is stopped”;
//display information for a CPU
};
#include <bits/stdc++.h>
#define cl(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1e5+50;
enum CPU_Rank {P1=1,P2,P3,P4,P5,P6,P7};
class CPU
{
CPU_Rank rank;
int frequency;
float voltage;
public:
CPU(int f, float v);
~CPU()
{
printf("destructure a object\n");
};
void Run()
{
printf("CPU is runing\n");
}
void Stop()
{
printf("CPU is stopped\n");
}
void display()
{
printf("frequency = %d ,voltage = %f\n",frequency,voltage);
}
void setnumf(int f)
{
frequency=f;
}
void setnumv(float t)
{
voltage=t;
}
int getF()
{
return frequency;
}
float getV()
{
return voltage;
}
//constructor for initialized instance varibles and display “construct a object”
//Destructor for display “destructure a object”;
//Get and set functions for each instance varible of the class
//Run() to display “CPU is runing”;
// Stop() to display CPU is stopped”;
//display information for a CPU
};
CPU::CPU(int f,float v)
{
frequency=f;
voltage=v;
printf("construct a object\n");
}
int main()
{
CPU a(10,1.3);
a.Run();
a.display();
a.Stop();
a.setnumf(7);
a.display();
a.setnumv(10.5);
a.display();
int f=a.getF();
cout<<f<<endl;
float t=a.getV();
cout<<t<<endl;
a.~CPU();
return 0;
}