C++习题03_类和对象

C++习题03_类和对象

习题03(01)计算器类

题目描述
下面是一个计算器类的定义,请完成该类成员函数的实现,并编写main()。
class Counter
{
public:
	Counter(int num);  
	void increament();  //给原值增1
	void decrement();  //给原值减1
    int getValue();     //取得计数器值
	void print();       //显示计数
private:
	int value;
}; 
输入描述
一个整数,用于创建Counter对象 
输出描述
调用成员函数进行相关操作后的计算器值 
输入样例
5 
输出样例
计数器的值为:7
计数器的值为:6
#include <iostream>
#include <iomanip>
using namespace std;

class Counter
{
public:
    Counter(int num);
    void increament(); //给原值增1
    void decrement();  //给原值减1
    int getValue();    //取得计数器值
    void print();      //显示计数
private:
    int value;
};

Counter::Counter(int num) : value(num) //value(num)等价于value=num;
{
}

void Counter::increament() //返回值   作用范围:: 函数名
{
    value++;
}

void Counter::decrement()
{
    value--;
}

int Counter::getValue()
{
    return value;
}

void Counter::print()
{
    cout << "计数器的值为:" << value << endl;
}

int main()
{
    int n;
    cin >> n;
    Counter k(n);
    k.increament();
    k.increament();
    k.print();
    k.decrement();
    k.print();
    return 0;
}

习题03(02)Date类

题目描述
根据注释语句的提示,实现类Date的成员函数,并编写main()
class Date
{
public:
	void setYear(int y);     //设置年的值
	void setMonth(int m);  //设置月的值
	void setDay(int d);    //设置日的值
	bool isLeapYear();    //判断是否是闰年
	void printDate();     //显示日期
private:
	int year,month,day;
}; 
输入描述
三个整数:年、月、日 
输出描述
年月日及该年是否是闰年 
输入样例
以下为两组测试数据
2017 9 20

2012 9 20 
输出样例
以下为两组测试数据对应的结果
日期为:2017年9月20日
该年为非闰年!

日期为:2012年9月20日
该年为闰年!
#include <iostream>
#include <iomanip>
using namespace std;

class Date
{
public:
	void setYear(int y);     //设置年的值
	void setMonth(int m);  //设置月的值
	void setDay(int d);    //设置日的值
	bool isLeapYear();    //判断是否是闰年
	void printDate();     //显示日期
private:
	int year, month, day;
};

void Date::setYear(int y)
{
	year = y;
}
void Date::setMonth(int m)
{
	month = m;
}
void Date::setDay(int d)
{
	day = d;
}
bool Date::isLeapYear()
{
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		return true;
	else
		return false;
}
void Date::printDate()
{
	cout << "日期为:" << year << "年" << month << "月" << day << "日" << endl;
}

int main()
{
	Date day;
	int y, m, d;
	cin >> y >> m >> d;
	day.setYear(y);
	day.setMonth(m);
	day.setDay(d);
	day.printDate();
	if (day.isLeapYear())
		cout << "该年为闰年!" << endl;
	else
		cout << "该年为非闰年!" << endl;
	return 0;
}
判断闰年

1. 可以被400整除
2. 可以被4整除但是不能被100整除

习题03(03)Cylinder类

题目描述
编程:建立Cylinder类,有三个double型私有数据成员:半径、高和体积,构造函数传递两个值:半径和高,计算体积。成员函数showVolume()用来显示每个对象的体积。
要求:半径、高从键盘输入。
PI的值取:3.1415926 
输入描述
两个实型数据:半径和高 
输出描述
一个数据:圆柱体的体积 
输入样例
2 3 
输出样例
体积为:37.6991
#include <iostream>
#include <iomanip>
using namespace std;

class Cylinder
{
public:
    Cylinder(double a, double b);
    void showVolume();

private:
    double h;
    double r;
    double v;
    double pi;
};

Cylinder::Cylinder(double a, double b)
{
    r = a;
    h = b;
    pi = 3.1415926;
}
void Cylinder::showVolume()
{
    v = h * pi * r * r;
    cout << "体积为:" << v << endl;
}

int main()
{
    double a, b;
    cin >> a >> b;
    Cylinder f(a, b);
    f.showVolume();
    return 0;
}

习题03(04)Book类

题目描述
构建一个Book类,有4个私有数据成员:书名、作者、qu和price(均为int型),将qu初始化为1~5,将price被始化为qu的10倍。建立一个有5个元素的对象数组,顺序显示每个对象数组中元素的信息;定义对象指针,通过指针访问对象数组,逆序显示对象数组中元素的信息。要求:书名、作者、qu的信息从键盘输入 
输入描述
5个对象数组元素的值 
输出描述
顺序显示对象数组中各对象的值
逆序显示对象数组中各对象的值 
输入样例
C语言程序设计 苏小红 2
C++程序设计 刘丽华 3
Python机器学习 范淼 4
数据结构与算法 徐凤生 3
大话数据结构 程杰 2 
输出样例
顺序显示的结果为:
书名:C语言程序设计     作者:苏小红    qu*price:40
书名:C++程序设计       作者:刘丽华    qu*price:90
书名:Python机器学习    作者:范淼      qu*price:160
书名:数据结构与算法    作者:徐凤生    qu*price:90
书名:大话数据结构      作者:程杰      qu*price:40

使用指针,逆序显示的结果为:
书名:大话数据结构      作者:程杰      qu*price:40
书名:数据结构与算法    作者:徐凤生    qu*price:90
书名:Python机器学习    作者:范淼      qu*price:160
书名:C++程序设计       作者:刘丽华    qu*price:90
书名:C语言程序设计     作者:苏小红    qu*price:40
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Book
{
private:
    string name;
    string writer;
    int qu;
    int price;

public:
    Book(string a = "", string b = "", int c = 0);
    void set(string a, string b, int c);
    void show();
};

Book::Book(string a, string b, int c)
{
    name = a;
    writer = b;
    qu = c;
    price = c * 10;
}
void Book::set(string a, string b, int c)
{
    name = a;
    writer = b;
    qu = c;
    price = c * 10;
}
void Book::show()
{
    cout << "书名:" << name << "\t"
         << "作者:" << writer << "\t"
         << "qu*price:" << qu * price << "\t" << endl;
}

int main()
{
    Book a[5];
    Book *p;
    p = a;
    p = p + 4; //向后移动了4个的单位
    int i, qu;
    string name, writer;
    for (i = 0; i < 5; i++)
    {
        cin >> name >> writer >> qu;
        a[i].set(name, writer, qu);
    }
    cout << "顺序显示的结果为:" << endl;
    for (i = 0; i < 5; i++)
    {
        a[i].show();
    }
    cout << "使用指针,逆序显示的结果为:" << endl;
    for (i = 0; i < 5; i++)
    {
        (*p).show();
        p--;
    }
    return 0;
}

习题03(05)Stock类

题目描述
编程:构建一个类Stock,含有字符数组stockcode[]及int型quan、double型成员price.
构造函数含有三个参数:字符数组na[]及q、p。当定义Stock的类对象时,将对象的第1个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quan、double。未设置第2和第3个参数时,quan的值为1000,price的值为9.98。成员函数print()无形参,要求使用this指针显示对象数据成员的内容。main()中定义二个对象,
对象s1的三个参数分别为: "600001”, 3000, 5.67,都由键盘输入
对象s2的第1个数据成员值为:"600002”, 由键盘输入,其余使用默认值。 
输入描述
两行数据
第一行为第一个对象的数据成员值,分别为:"600001”, 3000, 5.67
第二行为第二个对象的第1个数据成员值,为:"600002” 
输出描述
两行
第一行为对象1的数据成员值
第二行为对象2的数据成员值 
输入样例
600001 3000 5.67
600002 
输出样例
stockcode:600001 quan:3000 price:5.67
stockcode:600002 quan:1000 price:9.98
#include <iostream>
#include <string>
using namespace std;

class Stock
{
private:
    string stockcode;
    int quan;
    double price;

public:
    Stock(string a, int b = 1000, double c = 9.98);
    void print();
};

Stock::Stock(string a, int b, double c)
{
    stockcode = a;
    quan = b;
    price = c;
}
void Stock::print()
{
    cout << "stockcode:" << stockcode << " quan:" << quan << " price:" << price << endl;
}

int main()
{
    string a, b;
    int c;
    double d;
    cin >> a >> c >> d;
    cin >> b;
    Stock one(a, c, d);
    Stock two(b);
    one.print();
    two.print();
    return 0;
}

习题03(06)Student类

题目描述
声明一个Student类,在该类的结构如下:
数据成员:
-stuNo:string
-name:string
-score:int  // 成绩
-sum:int   // 静态数据成员,统计总分
-count:int    //静态数据成员,统计学生人数、
成员函数
+setData(no:string,na:string,sco:int):void  //设置数据成员的值
+<<static>>getSum():int   //返回学生成绩之和
+<<static>>getCount():int   //返回学生总人数
+<<static>>calAverage():double   //计算平均分
+show():void    //输出学生学号、姓名、成绩
+<<static>>showStaticData():void //输出学生的总人数、总分
在main()函数中,输入某班若干位同学(人数不超过30)的信息,并求出全班学生的总人数、总分及平均分。

本题每组测试用例的对象数是不固定的,以组合键Ctrl+Z或人数>30结束输入,例如:
while((cin>>stuNo>>name>>score)&&Student::getCount()<30)	{	
		stu[Student::getCount()].setData(stuNo,name,score);	
} 
输入描述
若干个学生的信息,每位学生信息一行 
输出描述
创建对象前的学生人数和总分信息
每位学生的信息
学生总人数、总分
学生平均分 
输入样例
2001 张翼德 87
2002 赵子龙 76
2003 黄汉升 85
2004 关云长 89
2005 马孟起 68 
输出样例
创建对象前,学生总人数、总分为:
现在有0人,总分为:0
学生成绩信息:
学号:2001 姓名:张翼德 成绩:87
学号:2002 姓名:赵子龙 成绩:76
学号:2003 姓名:黄汉升 成绩:85
学号:2004 姓名:关云长 成绩:89
学号:2005 姓名:马孟起 成绩:68
现在有5人,总分为:405
学生的平均分为:81
#include <iostream>
#include <string>
using namespace std;

class Student
{
private:
    string stuNo;
    string name;
    int score;
    static int sum; //加个static就是静态
    static int count;

public:
    void setData(string no, string na, int sco);
    void show();
    static int getSum();
    static int getCount();
    static double calAverage();
    static void showStaticData();
};
void Student::setData(string no, string na, int sco)
{
    stuNo = no;
    name = na;
    score = sco;
    count++;
    sum += score;
}
void Student::show()
{
    cout << "学号:" << stuNo << " 姓名:" << name << " 成绩:" << score << endl;
}
int Student::getCount()
{
    return count;
}
int Student::getSum()
{
    return sum;
}
double Student::calAverage()
{
    double k;
    k = double(sum) / double(count);
    return k;
}
void Student::showStaticData()
{
    cout << "现在有" << count << "人,总分为:" << sum << endl;
}
int Student::count = 0;
int Student::sum = 0;

int main()
{
    cout << "创建对象前,学生总人数、总分为:" << endl;
    Student::showStaticData();
    Student stu[30];
    string a;
    string b;
    int c;
    while ((cin >> a >> b >> c) && Student::getCount() < 30)
    {
        stu[Student::getCount()].setData(a, b, c);
    }
    cout << "学生成绩信息:" << endl;
    int i = 0;
    for (i; i < Student::getCount(); i++)
    {
        stu[i].show();
    }
    Student::showStaticData();
    double k = Student::calAverage();
    cout << "学生的平均分为:" << k << endl;
    return 0;
}
  • 18
    点赞
  • 125
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bmNkotc2AECynaY6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值