C++练习
教材:c++语言程序设计第四版
[教材习题4_13:定义Circle类并计算面积]
【问题描述】
定义一个Circle类,有数据成员radius(半径),成员函数getArea()计算圆的面积。构造一个Circle的对象进行测试(注:圆周率取值3.14)。
#include<iostream>
using namespace std;
const float PI = 3.14;
class Circle{
public:
Circle(float r);
void area();
private:
float radius;
};
Circle::Circle(float r) {
radius = r;
}
void Circle::area() {
cout <<"Area:"<< PI * radius * radius << endl;
}
int main() {
float radius;
cout << "Input Radius:";
cin >> radius;
Circle a(radius);
a.area();
}
[教材习题4_8:定义Dog类,包含age和weight信息]
【问题描述】
定义一个Dog类,包含age,weight等属性,以及对这些属性操作的方法。实现并测试这个类。
【输入形式】
程序参考的输入(提示“Input Age and Weight:”):
Input Age and Weight:3 20
#include<iostream>
using namespace std;
class Dog {
public:
Dog(int nage, int nweight);
void print();
private:
int age, weight;
};
Dog::Dog(int nage, int nweight) {
age = nage;
weight = nweight;
}
void Dog::print() {
cout << "Dog Age:" << age <<" "<<"years" << endl;
cout << "Dog Weight:" << weight << "kg" << endl;
}
int main() {
int age, weight;
cout << "Input Age and Weight:";
cin >> age >> weight;
Dog a(age, weight);
a.print();
return 0;
}
[教材习题4_9:通过屏幕两点确定矩形类Rectangle]
【问题描述】设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算矩形的面积。
【输入形式】程序参考的输入(提示“Input Point A:”输入左下角的坐标;提示“Input Point B:”输入左下角的坐标):
Input Point A:0 0
Input Point B:8.2 4.5
#include <iostream>
using namespace std;
class Rectangle {
public:
void getPoint();
float getArea();
private:
double x1;
double x2;
double y1;
double y2;
double Area;
};
void Rectangle::getPoint() {
cout << "Input Point A:";
cin >> x1 >> y1;
cout << "Input Point B:";
cin >> x2 >> y2;
}
float Rectangle::getArea() {
Area = (x1 - x2) * (y1 - y2);
if (Area < 0) {
Area = -Area;
}
return Area;
}
int main()
{
Rectangle a;
a.getPoint();
float area = a.getArea();
cout << "Rectangle Area:" << area << endl;
return 0;
}
[ 教材习题4_20:定义满足要求的复数类Complex类]]
[教材习题4_20:定义满足要求的复数类Complex类]
【问题描述】
定义一个复数类Complex,使得下面的代码能够工作。(注:下列代码需放在主函数中。)
Complex c1(3,5); //用复数3+5i初始化c1
Complex c2=4.5; //用实数4.5初始化c2
c1.add(c2); //将c1与c2相加,结果保存在c1中
c1.show(); //将c1输出(这时的结果应该是7.5+5i)
#include<iostream>
using namespace std;
class Complex {
public:
Complex(float nsshu = 0, float nxshu = 0) {
sshu = nsshu;
xshu = nxshu;
}
void add(Complex c1) { sshu += c1.sshu; xshu += c1.xshu; };
void show();
private:
float sshu, xshu;
};
void Complex::show() {
cout << sshu << "+" << xshu << "i" << endl;
}
int main() {
Complex c1(3, 5); //用复数3+5i初始化c1
Complex c2 = 4.5; //用实数4.5初始化c2
c1.add(c2); //将c1与c2相加,结果保存在c1中
c1.show(); //将c1输出(这时的结果应该是7.5+5i)
return 0;
}
[实验题2.2定义Employee类,输出编号和姓名]
【问题描述】
定义一个Employee类,在Employee类中增加一个数据(静态数据成员)来设置本公司员工编号基数,新增加的员工编号将在创建对象的同时自动在基数上增加(程序输入2个人员)。
【输入形式】
参考的输入如下(提示“Input name:”):
Input name:zhangsan
Input name:lisi
#include <iostream>
#include <string>
using namespace std;
class Employee
{
public:
Employee();
void display();
private:
static int base_id;
int id;
string name;
};
int Employee::base_id = 1000;
Employee::Employee() {
cout << "Input name:";
cin >> name;
id = ++base_id;
}
void Employee::display() {
cout << "ID:" << id << " " << "Name:" << name << endl;
}
int main()
{
Employee e1;
Employee e2;
e1.display();
e2.display();
return 0;
}
[实验题2.1利用对象数组,计算10个顶点组成折线的长度]
【问题描述】
假设有一个点类point,具有两个实数坐标。希望主程序使用这个类完成下述功能:
(l)主程序为类point定义10个点的对象数组(也可以动态申请空间)。
(2)要求调用一个函数Set()从键盘输入10个对象的属性。
(3)要求调用一个函数Display()显示10个对象的值。
(4)要求调用一个函数Lenth(),计算将这些点连成一条折线时,这条折线的长度。
#include<iostream>
#include <stdlib.h>
#include"math.h"
using namespace std;
class Point {
public:
void Set();
void Display();
void Length(Point a[]);
private:
int x, y;
};
void Point::Set() {
cout << "Input x,y:";
cin >> x >> y;
}
void Point::Display() {
cout << "(" << x << "," << y << ")" << endl;;
}
void Point::Length(Point a[]) {
double length = 0;
for (int i = 0; i < 9; i++) {
length += sqrt(abs(a[i + 1].x - a[i].x) * abs(a[i + 1].x - a[i].x) + abs(a[i + 1].y - a[i].y) * abs(a[i + 1].y - a[i].y));
}
cout<<"Length:" << length;
}
int main() {
Point a[10];
for (int i = 0; i < 10; i++) {
a[i].Set();
}
for (int i = 0; i < 10; i++) {
a[i].Display();
}
a->Length(a); //a[10].Length(a);
[教材习题5_7:利用静态变量统计小猫Cat的数量]
定义一个Cat类,拥有静态数据成员numOfCats,记录Cat的个体数目;静态成员函数getNumOfCats(),读取numOfCats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法。
主函数如下:
int main()
{
Cat c;
cout<<“Num:”<<Cat::getNumOfCats()<<endl;
Cat carr[4];
cout<<“Num:”<<Cat::getNumOfCats()<<endl;
return 0;
}
#include <iostream>
using namespace std;
class Cat {
public:
Cat() {
numOfCats++;
}
~Cat() { numOfCats--; }
static int getNumOfCats() {
return numOfCats;
}
private:
static int numOfCats;
};
int Cat::numOfCats = 0;
int main()
{
Cat c;
cout << "Num:" << Cat::getNumOfCats() << endl;
Cat carr[4];
cout << "Num:" << Cat::getNumOfCats() << endl;
return 0;
}
[教材习题5_13:定义类X,Y,Z,按要求实现友元]
【问题描述】
定义类X,Y,Z,函数h(X*),满足:类X有私有成员i,Y的成员函数g(X*)是X的友元函数,实现对X的成员i加1;类Z是类X的友元类,其成员函数f(X*)实现对X的成员i加5;函数h(X*)是X的友元函数,实现对X的成员i加10。主函数实现测试。主函数需要定义如下:
int main()
{
int a;
cin>>a;
X x(a);
Y y;
Z z;
y.g(x);
z.f(x);
h(x);
x.print();
}
#include <iostream>
using namespace std;
class X;
class Y
{
public:
void g(X & x);
};
class Z
{
public:
void f(X & x);
};
class X
{
public:
X(int i) :i(i) {}
friend void Y::g(X & x);
friend class Z;
friend void h(X & x);
void print()
{
cout << i;
}
private:
int i;
};
void Y::g(X & x) {
x.i += 1;
}
void Z::f(X& x) {
x.i += 5;
}
void h(X& x) {
x.i += 10;
}
int main()
{
int a;
cin >> a;
X x(a);
Y y;
Z z;
y.g(x);
z.f(x);
h(x);
x.print();
}
[教材习题5_14:定义Boat与Car两个类,友元计算总重量]
【问题描述】
定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数getTotalWeight(),计算二者的重量和。
【输入形式】
参考的输入(数字前为提示文字):
Input boat weight:3
Input car weight:5
#include <iostream>
using namespace std;
class Car;
class Boat {
public:
Boat(int weight) {
weightB = weight;
}
friend int getTotalWeight( Car & a,Boat & b);
private:
int weightB;
};
class Car {
public:
Car(int weight) {
weightC = weight;
}
friend int getTotalWeight( Car & a, Boat & b);
private:
int weightC;
};
int getTotalWeight(Car & a,Boat & b) {
return static_cast<int>(a.weightC + b.weightB);
}
int main() {
int a,b;
cout << "Input boat weight:";
cin >> a;
Boat test1(a);
cout << "Input car weight:";
cin >> b;
Car test2(b);
cout<<getTotalWeight(test2, test1)<<endl;
return 0;
}
教材习题6_20:实现一个简单圆类
【问题描述】
实现一个名为SimpleCircle的简单圆类。其数据成员int *itsRadius为一个指向其半径值的指针,存放其半径值。设计对数据成员的各种操作并计算面积,给出这个类的完整实现并测试这个类。
注:简单计算半径为10的圆面积,不需要输入,圆周率取3.14。
参考的输出如下:
Area:314
#include <iostream>
using namespace std;
const float pi = 3.14;
class SimpleCircle
{
public:
SimpleCircle() {
itsRadius=new float();
//也可以这样分配内存:itsRadius=(float*)malloc(sizeof(float));
//但是要加上头文件:include<stdio.h>
}
void setRadius(float r)
{
*itsRadius = r;
}
float getRadius()
{
return *itsRadius;
}
float getArea()
{
return (*itsRadius) * (*itsRadius) * pi;
}
private:
float* itsRadius;
};
int main()
{
SimpleCircle c;
c.setRadius(10);
cout << "Area:" << c.getArea() << endl;
}
教材习题7_5:Shape类派生Rectangle和Circle类
问题描述】
定义一个基类Shape,在此基础上派生出Rectangle和Circle,二者都有getArea()函数计算对象面积。使用Rectangle类创建一个派生类Square。
注:圆周率取3.14
【输入形式】
参考的输入(数字前面的文字为提示):
Input a,b:5 10
Input r:10
【输出形式】
参考的输出:
Rectangle Area:50,Circle Area:314
#include<iostream>
using namespace std;
#define PI 3.14
class Shape {
public:
float getArea() {};
private:
};
class Rectangle:public Shape {
public:
Rectangle(float nwidth, float nlength) {
width = nwidth;
length = nlength;
}
float getArea() {
return width * length;
}
private:
float width, length;
};
class Circle:public Shape {
public:
Circle(float nr) {
r = nr;
}
float getArea() {
return PI * r * r;
}
private:
float r;
};
int main() {
float a, b,r;
cout << "Input a,b:";
cin >> a >> b;
cout << "Input r:";
cin >> r;
Rectangle* p1 = new Rectangle(a, b);
Circle* p2 = new Circle(r);
cout <<"Rectangle Area:" << p1->getArea() << ",Circle Area:" << p2->getArea();
return 0;
}
教材习题7_6:哺乳动物类Mammal派生出狗类Dog
【问题描述】
定义一个哺乳动物类Mammal,再由此派生出狗类Dog,定义一个Dog类的对象,观察基类与派生类的构造函数和析构函数的调用顺序。
【输入形式】
参考的输入为("Input Dog Name:"为提示文字):
Input Dog Name:wangcai
#include <iostream>
#include <string>
using namespace std;
class Mammal
{
public:
Mammal(string name) :name(name)
{
cout << "Con.Mammal" << endl;
}
~Mammal()
{
cout << "Des.Mammal" << endl;
}
protected:
string name;
};
class Dog:public Mammal {
public:
Dog(string name) :Mammal(name) {
cout << "Con.Dog:" << name << endl;
}
~Dog(){
cout << "Des.Dog:" << name<<endl;
}
private:
};
int main()
{
string name;
cout << "Input Dog Name:";
cin >> name;
Dog d(name);
return 0;
}