1
题目描述
定义一个 Lingjian 类,拥有整型的数据成员 Weight 和静态数据成员 AllWeights(初始化为 0),每定义一个对象时, 在AllWeights 中增加该零件的重量 Weight;在析构函数中减去 Weight;静态成员函数 GetAllWeights()获取AllWeights。设计程序,定义两个对象之后, 输出类的 AllWeights。其中 Weight 是通过成员函数输入对单个零件重量赋值。
源代码
#include <iostream>
using namespace std;
class Lingjian
{
private:
int Weight;
static int AllWeights;
public:
static int GetAllWeights()
{
cout << AllWeights << endl;
}
Lingjian(int weight)
{
Weight = weight;
AllWeights += Weight;
}
~Lingjian()
{
AllWeights -= Weight;
}
};
int Lingjian::AllWeights = 0;
int main()
{
int a,b;
cin>> a>> b;
Lingjian P1(a),P2(b);
Lingjian::GetAllWeights() ;
return 0;
}
2
题目描述
定义狮子 Lion与老虎 Tiger 两个类,二者都有 weight 私有整型属性,定义二者的一个友元函数totalWeight(参数表),计算二者的重量和。设计程序,定义狮子与老虎两个对象之后,调用 totalWeight(参数表),计算二者的重量和,然后输出。其中狮子与老虎的 Weight 是在各自的类成员函数中读数赋值。
源代码
#include <iostream>
using namespace std;
class Tiger;
class Lion;
class Lion
{
private:
int Weight;
public:
Lion(int weight)
{
Weight = weight;
}
friend int totalWeight(Lion &P1,Tiger &P2);
};
class Tiger
{
private:
int Weight;
public:
Tiger(int weight)
{
Weight = weight;
}
friend int totalWeight(Lion &P1,Tiger &P2);
};
int totalWeight(Lion &P1,Tiger &P2)
{
cout << (P1.Weight + P2.Weight);
}
int main()
{
int l,t;
cin >> l >>t;
Lion P1(l);
Tiger P2(t);
totalWeight(P1,P2);
return 0;
}
3
题目描述
集合是由一个或多个确定的元素所构成的整体。集合的运算有并、交、相对补等。
集合A和集合B的交集:由属于A且属于B的相同元素组成的集合。
集合A和集合B的并集:由所有属于集合A或属于集合B的元素所组成的集合。
集合B关于集合A的相对补集,记做A-B:由属于A而不属于B的元素组成的集合。
假设集合A={10,20,30},集合B={1,10,50,8}。则A与B的并是{10,20,30,1,50,8},A与B的交是{10},B关于A的相对补集是{20,30}。
定义整数集合类CSet,属性包括:集合中的元素个数n,整型指针data存储集合中的元素。
主函数输入集合A、B的数据,计算集合的并、交、相对补。
可根据题目,为CSet类添加需要的成员函数。
方法有:重载输出,按样例格式输出集合中的元素。
重载+运算符,求集合A和集合B的并集,并返回结果集合。 重载-运算符,求集合B关于集合A的相对补集,并返回结果集合。 重载*运算符,求集合A和集合B的交集,并返回结果集合。
源代码
#include<iostream>
using namespace std;
class CSet
{
public:
CSet(){}
CSet(int n1,int *p)
{
n=n1;
for(int i=0;i<n;i++)
data[i]=p[i];
}
void display()
{
for(int j=0;j<n;j++)
{
cout<<data[j]<<" ";
}
cout<<endl;
}
CSet operator + (CSet &b);
CSet operator - (CSet &b);
CSet operator * (CSet &b);
private:
int n;
int data[100];
};
CSet CSet::operator - (CSet &b)
{
int i=0;
int a[100];
for(int j=0;j<n;j++)
{
int flag=0;
for(int k=0;k<b.n;k++)
{
if(data[j]==b.data[k])
{
flag=0;
break;
}
else
flag=1;
}
if(flag==1)
{
a[i]=data[j];
i++;
}
}
CSet c(i,a);
return c;
}
CSet CSet::operator * (CSet &b)
{
int i=0;
int mm[100];
for(int j=0;j<n;j++)
{
for(int k=0;k<b.n;k++)
{
if(data[j]==b.data[k])
{
mm[i]=data[j];
i++;
break;
}
}
}
CSet c(i,mm);
return c;
}
CSet CSet::operator +(CSet &b)
{
int i = 0;
int a[100];
int g[100] = {0};
int h = b.n;
for(int j = 0; j < h; j++)
g[j] = b.data[j];
for(int k=0;k<n;k++)
{
for(int j=0;j<h;j++)
{
if(data[k]==g[j])
{
for(j; j < h;j++)
g[j] = g[j+1];
h--;
break;
}
}
}
int d = 0;
for(d ; d< n;d++)
a[d] = data[d];
for(int j = 0;j< h;j++)
{
a[d] = g[j];
d++;
}
CSet c(d,a);
return c;
}
int main()
{
int t,n1,n2;
cin>>t;
for(int i=0;i<t;i++)
{
cin>>n1;
int p1[n1] = {0};
for(int j=0;j<n1;j++)
{cin>>p1[j];
}
CSet A(n1,p1);
cin>>n2;
int p2[n2] = {0};
for(int j=0;j<n2;j++)
cin>>p2[j];
CSet B(n2,p2);
cout<<"A:";
A.display();
cout<<"B:";
B.display();
CSet C=A+B;
cout<<"A+B:";
C.display();
CSet D=A*B;
cout<<"A*B:";
D.display();
CSet e=A-B;
CSet f=B-A;
CSet g=e+f;
cout<<"(A-B)+(B-A):";
g.display();
cout << endl;
}
return 0;
}
4
题目描述
要求定义一个基类Point,它有两个私有的float型数据成员X,Y;一个构造函数用于对数据成员初始化;有一个成员函数void Move(float xOff, float yOff)实现分别对X,Y值的改变,其中参数xOff和yOff分别代表偏移量。另外两个成员函数GetX() 、GetY()分别返回X和Y的值。
Rectangle类是基类Point的公有派生类。它增加了两个float型的私有数据成员W,H; 增加了两个成员函数float GetH() 、float GetW()分别返回W和H的值;并定义了自己的构造函数,实现对各个数据成员的初始化。
编写主函数main()根据以下的输入输出提示,完成整个程序。
源代码
#include <iostream>
using namespace std;
class Point
{
private:
float X,Y;
public:
Point(float x,float y)
{
X=x;
Y=y;
}
void Move(float xOff, float yOff)
{
X+=xOff;
Y+=yOff;
}
float GetX()
{
return X;
}
float GetY()
{
return Y;
}
};
class Rectangle:public Point
{
private:
float W, H;
public:
Rectangle(float X,float Y,float W,float H):Point(X, Y)
{
this->W = W;
this->H = H;
}
float GetH()
{
return H;
}
float GetW()
{
return W;
}
};
int main()
{
float X,Y,W,H,xOff,yOff;
cin >> X >> Y >> W >> H >> xOff >>yOff;
Rectangle P(X,Y,W,H);
P.Move(xOff,yOff);
cout << P.GetX() << " "<< P.GetY() << " "<< P.GetW() << " "<< P.GetH() ;
return 0;
}
5
题目描述
定义一个基类Person,它有3个protected的数据成员:姓名name(char *类型)、性别 sex(char类型)、年龄age(int类型);一个构造函数用于对数据成员初始化;有一个成员函数show()用于输出数据成员的信息。
创建Person类的公有派生类Employee,增加两个数据成员 基本工资 basicSalary(int类型) 请假天数leaveDays(int型);为它定义初始化成员信息的构造函数,和显示数据成员信息的成员函数show()。
源代码
#include <iostream>
#include <string.h>
using namespace std;
class Person
{
protected:
char name[30];
//char *name[30];
char sex;
int age;
public:
Person(char*Name,char Sex,int Age)
{
strcpy(name,Name);
sex = Sex;
age = Age;
}
void show()
{
cout << "name:" << name <<endl;
cout << "age:" << age << endl;
cout << "sex:" << sex << endl;
}
};
class Employee:Person
{
private:
int basicSalary,leaveDays;
public:
Employee(char*Name,char Sex,int Age,int BasicSalary,int LeaveDays):Person(Name,Sex,Age)
{
basicSalary = BasicSalary;
leaveDays = LeaveDays;
}
void show()
{
Person::show() ;
cout << "basicSalary:" << basicSalary << endl;
cout << "leavedays:" << leaveDays << endl;
}
};
int main()
{
char s;
int a,b,l;
char name[30];
cin>> name >> s >> a >> b >> l;
Employee P(name,s,a,b,l);
P.show() ;
return 0;
}