Assignment 8
来源:互联网 发布:淘宝店铺导航分类横着 编辑:程序博客网 时间:2024/05/01 12:01
1.分析下面的程序,写出运行结果。
#include <iostream>
using namespace std;
class Date
{
public:
Date(int,int,int);
Date(int,int);
Date(int);
Date();
void display();
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y):month(m),day(d),year(y){}
Date::Date(int m,int d):month(m),day(d){year=2005;}
Date::Date(int m):month(m)
{
day=1;
year=2005;
}
Date::Date()
{
month=1;
day=1;
year=2005;
}
void Date::display()
{
cout<<month<<"/"<<day<<"/"<<year<<endl;
}
int main()
{
Date d1(10,13,2005);
Date d2(12,30);
Date d3(10);
Date d4;
d1.display();
//////////////////////////////////////////////////////////////////////////
//10/13/2005
//////////////////////////////////////////////////////////////////////////
d2.display();
//////////////////////////////////////////////////////////////////////////
//12/30/2005
//////////////////////////////////////////////////////////////////////////
d3.display();
//////////////////////////////////////////////////////////////////////////
//10/1/2005
//////////////////////////////////////////////////////////////////////////
d4.display();
//////////////////////////////////////////////////////////////////////////
//1/1/2005
//////////////////////////////////////////////////////////////////////////
return 0;
}
2. 编写设计一个People类。该类的数据成员有年龄(age)、身高(height)、体重(weight)、函数成员有进食(eat)、运动(sport)、睡眠(sleep)。其中进食函数使体重加1,运动函数使身高加1,睡眠函数使年龄、身高、体重各加1。要求所有数据成员为private,成员函数为public访问权限。
程序代码:
#include <iostream>
using namespace std;
class People
{
public:
People(int iAge = 0,double dHeight =0,double dWeight = 0);
int eat();
double sport();
void sleep();
void display();
private:
int iAge;
double dHeight;
double dWeight;
};
People::People(int a,double h,double w)
{
iAge = a;
dHeight = h;
dWeight = w;
}
void People::display()
{
cout << iAge << "" << dHeight << ' ' << dWeight << endl;
}
int People::eat()
{
return ++dWeight ;
}
double People::sport()
{
return ++dHeight;
}
void People::sleep()
{
++iAge;
++dHeight;
++dWeight;
}
int main(int argc,char* argv[])
{
People p1(20,170,50);
cout << "该人的原年龄,身高(cm),体重(kg)为: " << endl;
p1.display();
cout << "吃饭后,体重的变化为:" << p1.eat() << endl;
cout << "运动后,身高的变化为:" << p1.sport() << endl;
cout << "睡觉后,年龄,身高,体重的变化为:";
p1.sleep();
p1.display();
return 0;
}
3.定义一个学生类,其中有3个数据成员学号、姓名、成绩,以及若干成员函数实现对学生数据的赋值和输出。
要求 (1)在主函数中定义一个对象数组,完成对数组中每个对象的赋值。
(2)定义一个函数找出成绩最高的学生的学号和姓名,该函数的形式参数为对象指针。
程序代码:
/*
3.定义一个学生类,其中有3个数据成员学号、姓名、成绩,以及若干成员函数实现对学生数据的赋值和输出。
要求 (1)在主函数中定义一个对象数组,完成对数组中每个对象的赋值。
(2)定义一个函数找出成绩最高的学生的学号和姓名,该函数的形式参数为对象指针。
*/
#include<iostream>
#include <string>
using namespace std;
class Student
{
public:
Student(int num ,string name,doublescore):iNum(num),name(name),dScore(score){}
void display();
void scoreSort(Student* st);
private:
int iNum;
string name;
double dScore;
};
void Student::display()
{
cout << "学号是:" << iNum << endl;
cout << "姓名是:" << name << endl;
cout << "成绩是:" << dScore << endl;
}
void Student::scoreSort(Student* st)
{
double tempMax = 0;
int j = 0;
tempMax = st[0].dScore;
for(int i = 0; i < 5;i ++)
{
if (st[i].dScore > tempMax)
{
tempMax =st[i].dScore;
j = i;
}
}
st[j].display();
}
int main(int argc,char* argv[])
{
Student stud[5] = {
Student(10001,"wanghai",89.2),
Student(10002,"xiaohai",92.2),
Student(10003,"fangtong",91.9),
Student(10004,"jinhai",88.3),
Student(10005,"guangliang",99.3)
};
for (int i = 0;i < 5;i ++)
{
stud[i].display();
}
cout <<"\n\n\n"<<endl;
cout << "分数最高的同学的:" << endl;
stud[0].scoreSort(stud);
return 0;
}
4.定义一个学生类,其中有3个私有数据成员学号、姓名、成绩,以及若干成员函数实现对学生数据的赋值和输出。
要求 (1)在主函数中定义一个对象数组,完成对数组中每个对象的赋值。
(2)定义一个友元函数找出成绩最高的学生的学号和姓名,该函数的形式参数为对象引用。
程序代码:
#include<iostream>
#include <string>
using namespace std;
class Student
{
public:
Student(int num ,string name,doublescore):iNum(num),name(name),dScore(score){}
void display();
friend void scoreSort(Student*st);
private:
int iNum;
string name;
double dScore;
};
void Student::display()
{
cout << "学号是:" << iNum << endl;
cout << "姓名是:" << name << endl;
cout << "成绩是:" << dScore << endl;
}
void scoreSort(Student* st)
{
double tempMax = 0;
int j = 0;
tempMax = st[0].dScore;
for(int i = 0; i < 5;i ++)
{
if (st[i].dScore > tempMax)
{
tempMax =st[i].dScore;
j = i;
}
}
st[j].display();
}
int main(int argc,char* argv[])
{
Student stud[5] = {
Student(10001,"wanghai",89.2),
Student(10002,"xiaohai",92.2),
Student(10003,"fangtong",91.9),
Student(10004,"jinhai",88.3),
Student(10005,"guangliang",99.3)
};
for (int i = 0;i < 5;i ++)
{
stud[i].display();
}
cout <<"\n\n\n"<<endl;
cout << "分数最高的同学的:" << endl;
scoreSort(stud);
return 0;
}
- Assignment 8
- Assignment
- Assignment 8: Network Flow Problems
- Assignment 1
- Assignment 2
- Assignment 3
- Assignment 4
- Assignment 5
- Assignment 6
- Assignment 7
- Assignment 9
- Assignment 10
- Number Assignment
- HDU5289 Assignment
- hdu5289 Assignment
- hdu5289 Assignment
- Assignment (HDU_5289)
- hdu5289 Assignment
- Assignment 4
- Assignment 5
- Assignment 6
- Assignment 7
- 各种C#数组的定义和初始化
- Assignment 8
- Assignment 9
- Assignment 10
- STL中遍历容器it++与++it的区别
- Java-3
- Java-2
- Nginx启动、重新启动命令
- Java-1
- mysql_fetch_row ,mysql_fetch_assoc. mysql_fetch_array的区别