C++第14章

Posted 自动秃头化

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++第14章相关的知识,希望对你有一定的参考价值。

1.

#include<iostream>
using namespace std;
class Box{
public:
int height;
int width;
int length;
int v;
Box(int a=10,int b=10,int c=10){
length=a;
width=b;
height=c;
cout<<"Constructor called!"<<endl;
}
Box(Box& rp){
length=rp.length;
width=rp.width;
height=rp.height;
cout<<"Copy constructor called!"<<endl;
}
~Box()
{
cout<<"Deconstructor called!"<<endl;
}
};
int main()
{
Box box1(15,30,25);
Box box2(10,10,10);
Box box3(box1);
cout<<"The volume of box1 is "<<box1.length*box1.width*box1.height<<endl;
cout<<"The volume of box2 is "<<box2.length*box2.width*box2.height<<endl;
cout<<"The volume of box3 is "<<box3.length*box3.width*box3.height<<endl;

}

2.

#include<iostream>
#include<cstring>
using namespace std;
class Student{
public:
int num;
char name[10];
char sex[3];
float score;
float pay=1500;
Student(int n=20010,char* na="Wang",char* s="m",float sc=89.5){
num=n;
strcpy(name,na);
strcpy(sex,s);
score=sc;
cout<<"student1: "<<endl;
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
cout<<"score: "<<score<<endl;
cout<<endl;

}
Student(Student &rp)
{
num=rp.num;
strcpy(name,rp.name);
strcpy(sex,rp.sex);
cout<<"teacher1: "<<endl;
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
cout<<"pay: "<<pay<<endl;
}

};
int main(){
Student student1(20010,"Wang","m",89.5);
Student teacher1=student1;
}

3.

#include<iostream>
using namespace std;
class Samp{
public:
int i;
int j;
Samp (){
int* pd=new int[10];
for(int i=0,j=0;i<10,j<10;i++,j++)
{
cout<<"Multi["<<i<<"] is: "<<i*j<<endl;
}
delete []pd;
}
void Setij();
~Samp(){
for(int i=9;i>=0;i--){
cout<<"Destroying: "<<i<<endl;
}
}
};

int main()
{
Samp s;
}

4.

#include<iostream>
using namespace std;
class Vector{
public:
int s;
Vector(int s){
int *pb=new int[s];
for(int i=0;i<s;i++)
{
cout<<i*i<<" ";
}
delete []pb;
}
Vector(Vector& rp){
int *pb=new int[10];
for(int j=0;j<10;j++)
{
cout<<j*j<<" ";
}
delete []pb;
}
~Vector(){
}
};
int main(){

Vector a(10);
Vector b(a);
}

5.

#include<iostream>
using namespace std;
class X{
public:
int m;
X(int p){
m=p;
cout<<"create a X object with "<<m<<endl;
}
X(X& rp){
m=rp.m;
cout<<"create a X object from other X object"<<endl;
}
~X(){
cout<<"erase a object"<<endl;
}
};
X f(X){
cout<<"call a func with para X object"<<endl;
cout<<"create a X object from other X object"<<endl;
}
int main()
{
X a(1);
a=f(a);
}

6.

#include<iostream>
#include<cstring>
using namespace std;
class Employee{
public:
char name[10];
int no;
Employee(char* na,int n){
strcpy(name,na);
no=n;
cout<<name<<":"<<no<<endl;
}
Employee(Employee& rp)
{
strcpy(name,rp.name);
no=rp.no+1;
cout<<name<<":"<<no<<endl;
}
};
int main()
{
Employee e1("Wang",0);
Employee e2(e1);
Employee e3(e2);
}

7.

#include<iostream>
#include<cstring>
using namespace std;
class Usetele{
public:
int id;
long int number;
int b;
char utype;
long int p;
Usetele(int i,long int n,char u)
{
id=i;
number=n;
utype=u;
if(id==0)
cout<<"Non-parameter constructor called!"<<endl;
else
cout<<"Constructor called!"<<endl;
}
Usetele(Usetele& rp){
id=rp.id;
number=rp.number;
utype=rp.utype;
cout<<"Copy constructor called!"<<endl;

}
void list()
{
cout<<"ID:"<<id<<endl;
cout<<"Tele:"<<number<<endl;
cout<<"Type:"<<utype<<endl;
}
void modity(int i,long int m,char u){
id=i;
number=m;
utype=u;
cout<<"6Information modified!"<<endl;
}
void Expand()
{
p=number;
int j;
j=p/1000000;
if(utype=='A')
{
if(j<=5) number=p+80000000;
if(j>5) number=p+70000000;}
if(utype=='B')
{
if(j<=5) number=p+60000000;
if(j>5) number=p+50000000;}
cout<<"Tele:"<<p<<"The new tele is:"<<number<<endl;
}

};
int main(){
Usetele a(0,0,'A');
Usetele b(6,8804455,'A');
Usetele c(b),d(21,4466223,'B'),e(d);
a.list();
b.modity(6,8804455,'A');
b.list();
b.Expand();
d.Expand();
b.list();
d.list();
}


以上是关于C++第14章的主要内容,如果未能解决你的问题,请参考以下文章

C++ Primer Plus编程练习答案——第13章

2018年java架构师分布式性能优化 附带源码

C/C++目录

《好学的C++ 第2版》 第1章 第一个c++程序

Java面向对象程序设计第14章3-8和第15章6

第14章 事件