习题四
建立一个对象数组,内放5个学生的数据(学号、成绩),请输出第1、3、5个学生的数据。
#include<iostream>
#include<string>
using namespace std;
const int COUNT = 5;
class student {
public:
void set_value();
friend void func(student Stu[],const int n);
private:
string num;
int score;
double mark;
};
void student::set_value() {//输入调用
cin >> num >> score;
}
void func(student Stu[], const int n) {//输出
int i = 0;
cout<<"学号--------- 成绩"<<endl;
for (i = 0; i< COUNT; i+=2) {
cout << Stu[i].num<<" "<< Stu[i].score << endl;
}
}
int main()
{
student stu[COUNT];
for (int i = 0; i < COUNT; i++)
{
stu[i].set_value();
}
func(stu, COUNT);
return 0;
}
习题五
建立一个对象数组,内放5个学生的数据(学号、成绩),编写一个友元函数func,在func里找出5个学生中成绩最高者,并输出其学号。
(本题基本与前一题相同,把顺序输出改为选择最大值输出即可,因此此处只贴出了不同的部分)
void func(student Stu[], const int n) {
int i = 0;
for (i = 0; i< COUNT; i++) {
if(Stu[i].score>=Stu[0].score){
Stu[0]=Stu[i];
}//此种方法未考虑两个人同时为最高分的可能
}
cout<<Stu[0].num<<endl;
}
程序补充
一、构造函数实现乘法
#include <iostream>
using namespace std;
class A{
public:
int value;
A(int n){//带参初始化
value = n;
}
// 你的代码添加在这里
A(const A &p){//复制初始化得到4倍的值
value=4* p.value;
}
};
int main()
{
int x;
cin >> x;
A a(x);//初始化输入值
A b = a;//得到4倍值
cout << b.value << endl;
return 0;
}
二、引用函数与构造函数
#include <iostream>
using namespace std;
class A {
public:
int v;
// 你的代码添加在这里
A(){
v=0;
}
int &value(){ //引用函数
return v;// 对于返回值n 有 &value=n;v=n;
//return this->v;//借用this指针来保证返回对象中的v
}
};
int main()
{
int n;
cin >> n;
A a;//构造函数创建对象
cout << a.v << endl;//只是调用构造函数 v为0
a.value() = n;//获得了值n 到函数value中
cout << a.v << endl; // 由value中的v输出n
return 0;
}
三、
#include <iostream>
using namespace std;
class A {
public:
int a;
A(int n):a(n) { }
};
class B {
public:
A obj;
int b;
// 在此处补充你的代码
B(int m):obj(m) { b=m;};
B(const B &p):obj(p.b){
b = p.b;
obj.a = 3 * p.obj.a;
}
};
int main()
{
int n;
cin >> n;
B a1(n);
B a2 = a1;
cout << a1.b << "," << a1.obj.a << endl;
cout << a2.b << "," << a2.obj.a << endl;
return 0;
}
构造函数实现学生类
请设计一个学生类,属性包括学号、姓名、性别、年龄、入学总成绩,
(为了展示构造函数的三种使用方法 实现私有变量的初始化)
#include<iostream>
#include<string>
using namespace std;
class student {
public:
student() {
cout << "调用默认构造函数" << endl;
}
student(string num, string name, string sex, int age, int score) {
cout << "调用带参数的构造函数" << endl;
m_num = num;
m_name = name;
m_sex = sex;
m_age = age;
m_score = score;
}
student(const student &p) {
cout << "调用复制构造函数" << endl;
m_num = p.m_num;
m_name = p.m_name;
m_sex = p.m_sex;
m_age = p.m_age;
m_score = p.m_score;
}
~student() {
cout << "调用了析构函数" << endl;
}
void set_value();
void display();
private:
string m_num;
string m_name;
string m_sex;
int m_age;
double m_score;
};
void student::set_value() {
cin >> m_num >> m_name >> m_sex >> m_age >> m_score;
}
void student::display() {
cout << "学号:" <<m_num<< endl;
cout << "姓名:" << m_name << endl;
cout << "性别:" << m_sex << endl;
cout << "年龄:" << m_age << endl;
cout << "入学总成绩:" << m_score << endl;
}
int main(){
student stu;
student stu2("2018141488888", "郭靖", "男", 20, 600);
stu.set_value();
student stu3 = stu2;
stu.display();
stu3.display();
return 0;
}
货物类
请设计一个货物类,计算所有货物的总重量。
#include<iostream>
#include<string>
using namespace std;
static int Totalweight;
class goods {
public:
void set(int m);
static void show_totalweight() {
cout <<"total weight:"<< Totalweight << endl;
}
goods(int x) {//带参构造函数
temp= x;
Totalweight += temp;
};
goods() {};//默认构造函数
goods(const goods &p) {//复制构造函数
Totalweight += p.temp;
}
private:
int temp;
};
void goods::set(int m) {
temp= m;
Totalweight = temp;
}
int main()
{
int n;
goods g1;
cin >> n;
g1.set(n);
goods::show_totalweight(); // 静态成员函数显示总重量
goods g2(200);//使用带参构造函数
goods::show_totalweight();
goods g3 = g2;//使用复制构造函数
goods::show_totalweight();
return 0;
}
实现日期类(基本类)
设计一个存储年月日的日期类,数据成员是私有的,成员函数是公有的,它有默认构造函数将数据成员初始化为0,一个set函数完成数据的赋值,一个display函数按“某年某月某日”的格式显示日期
#include<iostream>
using namespace std;
class Date {
public:
void display();
void set(int y, int m, int d);
Date() {
year = 0;
month = 0;
day = 0;
}
private:
int year;
int month;
int day;
};
void Date::set(int y, int m, int d) {
year = y;
month = m;
day = d;
}
void Date::display() {
cout << year << "-" << month << "-" << day << endl;
}
int main() {
int y, m, d;
cin >> y >> m >> d;
Date date;
date.display();
date.set(y, m, d);
date.display();
return 0;
}
实现一个复数类
#include<iostream>
using namespace std;
class Complex {
public:
void set(float r, float i=0);
void output();
Complex() {
real = 0, image = 0;
}
Complex(float a, float b=0) {
real = a, image = b;
}
private:
float real;
float image;
};
void Complex::output() {
if (real==0&&image!=0)cout << image << "i" << endl;//纯虚数
else if (image > 0&&real!=0) {
cout << real << "+" << image << "i" << endl;
}
else if(image<0&&real!=0){
cout << real << image<<"i" << endl;
}//虚数本身自带负号 不需要添加符号
else if(image==0)cout << real << endl;//纯实数
}
void Complex::set(float r, float i ) {
real=r;
image = i;
}
int main()
{
Complex a(1.2, 3.4);
a.output();
Complex b(-1.2, -3.4);
b.output();
Complex c;
c.output();
c.set(5);
c.output();
c.set(0, -5);
c.output();
return 0;
}
实现一个处理分数类
#include<iostream>
#include<cmath>
using namespace std;
class Fraction{
public:
Fraction(int a,int b=1){//如果输入没有b的话 默认为1
top=a;den=b;
}//带参初始化
void output();
void set(int a,int b);
private:
int top;
int den;
};
void swap(int &x, int &y) {
int tem;
tem = x;
x = y;
y = tem;
}
int calculator(int a, int b) {//求最大公因数来化简分数
int tem1 = abs(a), tem2 = abs(b);
while (tem1 != tem2) {
if (tem1 > tem2) {
tem1 = tem1 - tem2;
}
else {
swap(tem1, tem2);
}
}
return tem1;
}
void Fraction::set(int a,int b){//读入函数
top=a;den=b;
}
void Fraction::output(){
//int x=calculator(top,den);不能把函数单独提出来
if(den==0){//分母不为0
cout<<"error"<<endl;
}
else if(den==1){//分母为1的整数
cout<<top<<endl;
}
else if (abs(top)>abs(den)){//假分数
if (top < 0 || den < 0) {
cout <<top/den<<" "<<abs(top) % den << "/" << den << endl;
}
else {
cout <<top/den<<" "<< top % den << "/" << den << endl;
}
}
else if(abs(top)<abs(den)){ //真分数化简(可以考虑是否需要化简 )
if (top < 0 || den < 0) {
cout <<"-"<<abs(top)/calculator(top,den) << "/" << abs(den)/calculator(top,den) << endl;
}
else {
cout <<abs(top)/calculator(top,den) << "/" << abs(den)/calculator(top,den)<< endl;
}
}
}
int main() {
Fraction a(3);
a.output();
Fraction b(4, -6);
b.output();
Fraction c(10, 0);
c.output();
c.set(-10, 3);
c.output();
return 0;
}
字符串类
字符串类mystring的私有数据成员char *p用来存储一个字符串,请设计这个字符串类使得以下的main函数能正常编译执行。
#include<iostream>
#include<cmath>
using namespace std;
class mystring{
public:
mystring (string str);
void set(char a);
void show();
mystring(const mystring&m){
mml=0;
p=m.p;
len=m.len;
}
~mystring (){
cout<<"free memory!"<<endl;
}
private:
char *p;
int len;
int mml;
};
mystring::mystring(string str){
int x;
len=str.length();
p=new char [2*len+1];
for(x=0;x<len;x++){
p[x]=str[x];
}
p[x] = '\0';
mml = 1;
}
void mystring::set(char a) {
int i,j;
for (i = len + 1,j=0; i < 2 * len + 1; i++,j++) {
p[i] = p[j];
}
p[i] = '\0';
p[0] = a;
}
void mystring::show() {
int i = 0;
if (mml) {
for (i; i < len; i++) {
cout << p[i];
}
cout << endl;
}
else {
for (i=len+1; i <= 2*len; i++) {
cout << p[i];
}
cout << endl;
}
}
int main() {
mystring s1("hello");
mystring s2 = s1;
s1.set('H'); // 修改字符串的第一个字符
s1.show();
s2.show();
return 0;
}