C++第三章编程题

C++第三章编程题

一、 设计汽车类

题目:
设计一个Car类,它的数据成员要能描述一辆汽车的品牌,型号,出厂年份和价格,成员函数包括提供合适的途径来访问数据成员,在main()函数中定义类的对象并调用相应成员函数。
设计私有数据成员:
string brand;
string type;
int year;
double price;
公有成员函数:
构造函数: 默认品牌undefinition,默认型号undefinition,默认年份2000,默认价格 0;
获取数据成员的函数 :根据需要设计
使用如下的main()函数:

int main()
{
	Car car1("FIAT", "Palio", 2007, 6.5);
	cout << car1.GetBrand() << "|" << car1.GetType() << "|" << car1.GetYear() << "|" << car1.GetPrice() << endl;
	Car car2;
	cout << car2.GetBrand() << "|" << car2.GetType() << "|" << car2.GetYear() << "|" << car2.GetPrice() << endl;
	return 0;
}
#include<iostream>
#include<string>
using namespace std;
class Car 
{
private:
	string brand;
	string type;
	int year;
	double price;
public:
	Car(string b, string t, int y, double p);
	Car();
	string GetBrand();
	string GetType();
	int GetYear();
	double GetPrice();
	~Car();
};
Car::~Car()
{ }
Car::Car(string b, string t, int y, double p)
{
	brand = b;
	type = t;
	year = y;
	price = p;
}
Car::Car()
{
	brand = "undefinition";
	type = "undefinition";
	year = 2000;
	price = 0;
}
string Car::GetBrand()
{
	return brand;
}
string Car::GetType()
{
	return type;
}
int Car::GetYear()
{
	return year;
}
double Car::GetPrice()
{
	return price;
}
int main()
{
	Car car1("FIAT", "Palio", 2007, 6.5);
	cout << car1.GetBrand() << "|" << car1.GetType() << "|" << car1.GetYear() << "|" << car1.GetPrice() << endl;
	Car car2;
	cout << car2.GetBrand() << "|" << car2.GetType() << "|" << car2.GetYear() << "|" << car2.GetPrice() << endl;
	return 0;
}

运行结果:
FIAT|Palio|2007|6.5
undefinition|undefinition|2000|0

二、设计学生类

题目:
定义一个学生类,设计私有数据成员:
年龄:age; 姓名 string name;
公有成员函数:
构造函数 带参数的构造函数 Student(int a, string m);
不带参数的构造函数 Student();
改变数据成员值的函数 void SetMember(int a, string m);
获取数据成员值的函数 int Getage()
string Getname()

在main()中定义一个有3个元素的对象数组并分别初始化,然后输出对象数组的信息。
main 函数的内容如下,请直接复制使用

int main()
	{
		Student stu[3] = { Student(13,"wang") };   /*第一个元素用带参构造函数初始化;第二、三个元素由无参构造函数初始化,默认年龄为 0 ,姓名为 "unnamed"*/
		stu[2].Setname(12, "zhang");           /*修改第三个元素的数据成员值*/
		cout << stu[0].Getage() << "," << stu[0].Getname() << "\n";
		cout << stu[1].Getage() << "," << stu[1].Getname() << "\n";
		cout << stu[2].Getage() << "," << stu[2].Getname() << "\n"; /*这三句可改用一个循环*/
		return 0;
	}
#include<iostream>
#include<string>
using namespace std;
class Student
{
	int age;
	string name;
public:
	Student(int a, string m);
	Student();
	void Setname(int a, string m);
	int Getage();
	string Getname();
};
Student::Student(int a, string m)
{
	age = a;
	name = m;
}
Student::Student()
{
	age = 0;
	name = "unnamed";
}
void Student::Setname(int a, string m)
{
	age = a;
	name = m;
}
int Student::Getage()
	{
		return age;
	}
	string Student::Getname()
	{
		return name;
	}
	int main()
	{
		Student stu[3] = { Student(13,"wang") };   /*第一个元素用带参构造函数初始化;第二、三个元素由无参构造函数初始化,默认年龄为 0 ,姓名为 "unnamed"*/
		stu[2].Setname(12, "zhang");           /*修改第三个元素的数据成员值*/
		cout << stu[0].Getage() << "," << stu[0].Getname() << "\n";
		cout << stu[1].Getage() << "," << stu[1].Getname() << "\n";
		cout << stu[2].Getage() << "," << stu[2].Getname() << "\n"; /*这三句可改用一个循环*/
		return 0;
	}

运行结果:
13,wang
0,unnamed
12,zhang

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ddj-sun

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

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

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

打赏作者

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

抵扣说明:

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

余额充值