最近要复习一下C和C++的基础知识,于是计划把之前学过的谭浩强的《C程序设计》和《C++程序设计》习题重新做一遍。
编译环境为:操作系统32位Win7,编译工具VC++6.0
第八章:类和对象
8.1)调试程序使之能运行
#include
using namespace std;
//设置时间
void set_time(void);
//输出时间
void show_time(void);
class Time
{
public:
int m_hour;
int m_minute;
int m_sec;
};
Time t;
int main()
{
set_time();
show_time();
return 0;
}
//设置时间
void set_time(void)
{
cin >> t.hour;
cin >> t.minute;
cin >> t.sec;
}
//输出时间
void show_time(void)
{
cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}
8.2)修改8.1中程序,要求:
1.将数据成员改为私有
2.将输入和输出功能改由成员函数实现
3.在类体内定义成员函数
#include
using namespace std;
class Time
{
public:
//设定时间
void set_time(void)
{
cin >> this -> hour;
cin >> this -> minute;
cin >> this -> sec;
}
//输出时间
void show_time(void)
{
cout << this -> hour << ":" << this -> minute << ":" << this -> sec << endl;
}
private:
int hour;
int minute;
int sec;
};
Time t;
int main()
{
t.set_time();
t.show_time();
return 0;
}
8.3)在8.2的基础上进行如下修改:
在类体内声明成员函数,而在类外定义成员函数
#include
using namespace std;
class Time
{
public:
void set_time(void);
void show_time(void);
private:
int hour;
int minute;
int sec;
};
Time t;
//设置时间
void Time :: set_time(void)
{
cin >> this -> hour;
cin >> this -> minute;
cin >> this -> sec;
}
//输出时间
void Time :: show_time(void)
{
cout << this -> hour << ":" << this -> minute << ":" << this -> sec << endl;
}
int main()
{
t.set_time();
t.show_time();
return 0;
}
8.4)修改本章8.3.3节中的程序
a.cpp代码
#include
#include "student.h"
using namespace std;
int main()
{
Student *s = new Student();
s -> SetValue();
s -> Display();
return 0;
}
student.h代码
#include
using namespace std;
class Student
{
public:
void Display();
void SetValue();
private:
int m_num;
string m_name;
char m_sex;
};
student.cpp代码
#include
#include"student.h"
using namespace std;
void Student :: Display()
{
cout << "num:" << m_num << endl;
cout << "name:" << m_name << endl;
cout << "sex:" << m_sex << endl;
}
void Student :: SetValue()
{
cin >> m_num >> m_name >> m_sex;
}
注意:在VC++6.0中,需要把三个文件都放入到工程中
错误做法:
正确做法:
如果不能正确设置工程,则会在通过编译后造成链接错误:
--------------------Configuration: a - Win32 Debug--------------------
Compiling...
a.cpp
Linking...
a.obj : error LNK2001: unresolved external symbol "public: void __thiscall Student::Display(void)" (?Display@Student @@QAEXXZ)
a.obj : error LNK2001: unresolved external symbol "public: void __thiscall Student::SetValue(void)" (?SetValue@Student @@QAEXXZ)
Debug/a.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错.
a.exe - 1 error(s), 0 warning(s)
另外,在*.cpp中定义inline函数也会产生上述的链接错误
在打开程序时直接打开*.dsp文件,可以直接打开一个工程
8.5)将例8.4改写为一个多文件的程序:
1.将类定义放在头文件arraymax.h中
2.将成员函数定义在源文件arraymax.cpp中
3.主函数放在源文件a.cpp中
a.cpp代码
#include
#include"arraymax.h"
using namespace std;
int main()
{
ArrayMax arrmax;
arrmax.SetValue();
arrmax.MaxValue();
arrmax.ShowMax();
return 0;
}
arraymax.h代码
class ArrayMax
{
public:
void SetValue(); //设置数组元素值
void MaxValue(); //找出最大值
void ShowMax(); //输出最大值
private:
int array[10]; //整型数组
int max; //数组最大值
};
arraymax.cpp代码
#include
#include"arraymax.h"
using namespace std;
void ArrayMax :: SetValue()
{
int i;
for(i = 0; i < 10; i++)
{
cin >> array[i];
}
}
void ArrayMax :: MaxValue()
{
int i;
max = array[0];
for(i = 1; i < 10; i++)
{
if(max < array[i])
{
max = array[i];
}
}
}
void ArrayMax :: ShowMax()
{
cout << "max=" << max << endl;
}
8.6)需要长方体的体积,编一个基于对象的程序。数据成员上包括长、宽、高,用成员函数实现以下功能:
1.用键盘键入长、宽、高
2.计算长方体的体积
3.输出长方体的体积
#include
using namespace std;
class Cuboid
{
public:
//输入长宽高
inline void SetValue()
{
cin >> m_Length >> m_Width >> m_Height;
}
//计算体积
inline void CalcVolume()
{
m_Volume = m_Length * m_Width * m_Height;
}
//输出体积
inline void PrintVolume()
{
cout << "Volume:" << m_Volume << endl;
}
private:
int m_Length; //长
int m_Width; //宽
int m_Height; //高
int m_Volume; //体积
};
int main()
{
Cuboid *c = new Cuboid();
c -> SetValue();
c -> CalcVolume();
c -> PrintVolume();
return 0;
}
第九章:关于类和对象的进一步讨论
9.2)分析程序并写出输出结果
#include
using namespace std;
class Date
{
public:
Date(int, int, int);
Date(int, int);
Date(int);
Date();
void Display();
private:
int m_Month;
int m_Day;
int m_Year;
};
Date :: Date(int m, int d, int y): m_Month(m), m_Day(d), m_Year(y)
{
}
Date :: Date(int m, int d): m_Month(m), m_Day(d)
{
m_Year = 2005;
}
Date :: Date(int m): m_Month(m)
{
m_Day = 1;
m_Year = 2005;
}
Date :: Date()
{
m_Month = 1;
m_Day = 1;
m_Year = 2005;
}
void Date :: Display()
{
cout << m_Month << "/" << m_Day << "/" << m_Year << endl;
}
int main()
{
Date d1(10, 13, 2005);
Date d2(12, 30);
Date d3(10);
Date d4;
d1.Display();
d2.Display();
d3.Display();
d4.Display();
return 0;
}
输出结果:
9.3)修改9.2
#include
using namespace std;
class Date
{
public:
Date(int m, int d, int y);
void Display();
private:
int m_Month;
int m_Day;
int m_Year;
};
Date :: Date(int m = 1, int d = 1, int y = 1)
{
m_Month = m;
m_Day = d;
m_Year = y;
}
void Date :: Display()
{
cout << m_Month << "/" << m_Day << "/" << m_Year << endl;
}
int main()
{
Date d1(10, 13, 2005);
Date d2(12, 30);
Date d3(10);
Date d4 = Date();
d1.Display();
d2.Display();
d3.Display();
d4.Display();
return 0;
}
9.4)建立一个对象数组,内放5个学生数据(学号、成绩),用指针指向数组受元素,输出第1、3、5个学生的数据
#include
using namespace std;
class Student
{
public:
//构造函数
Student(int id, int score)
{
this -> m_Id = id;
this -> m_Score = score;
}
//打印信息
void PrintInfo()
{
cout << m_Id << ':' << m_Score << endl;
}
private:
int m_Id;
int m_Score;
}
stu[5] =
{
Student(1, 90),
Student(2, 80),
Student(3, 70),
Student(4, 60),
Student(5, 50)
};
int main()
{
stu[0].PrintInfo();
stu[2].PrintInfo();
stu[4].PrintInfo();
return 0;
}
9.5)建立一个对象数组,内放5个学生数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max中找出5个学生中成绩最高者并输出学号
#include
using namespace std;
class Student
{
public:
//构造函数1
Student()
{
this -> m_Id = -1;
this -> m_Score = 0;
}
//构造函数2
Student(int id, int score)
{
this -> m_Id = id;
this -> m_Score = score;
}
//打印信息
void PrintInfo()
{
cout << m_Id << ':' << m_Score << endl;
}
//获取分数信息
int GetScore()
{
return m_Score;
}
private:
int m_Id;
int m_Score;
};
//五个学生的数据
Student* stu[5] =
{
new Student(1, 90),
new Student(2, 80),
new Student(3, 70),
new Student(4, 60),
new Student(5, 50)
};
//找出5个学生中成绩最高者
void FindMax()
{
Student *max = stu[0];
int i;
for(i = 1; i < 5; i++)
{
if(max -> GetScore() < stu[i] -> GetScore())
{
max = stu[i];
}
}
max ->PrintInfo();
}
int main()
{
FindMax();
return 0;
}
9.6)阅读程序,写出输出结果
#include
using namespace std;
class Student
{
public:
Student(int n, float s): m_num(n), m_score(s) { }
void Change(int n, float s) { m_num = n; m_score = s; }
void Display() { cout << m_num << ":" << m_score << endl; }
private:
int m_num;
float m_score;
};
int main()
{
Student stud(101, 78.5);
stud.Display();
stud.Change(101, 80.5);
stud.Display();
return 0;
}
输出结果:
9.8)修改题6的程序,增加一个fun函数,改写main函数。在main函数中调用fun函数,在fun函数中调用change和display函数。在fun函数中使用对象的引用作为形参
#include
using namespace std;
class Student
{
public:
Student(int n, float s): m_num(n), m_score(s) { }
void Change(int n, float s) { m_num = n; m_score = s; }
void Display() { cout << m_num << ":" << m_score << endl; }
private:
int m_num;
float m_score;
};
void fun(Student& s)
{
s.Change(101, 80.5);
s.Display();
}
int main()
{
Student stud(101, 78.5);
stud.Display();
fun(stud);
return 0;
}
9.9)商店销售某一商品,每天公布统一折扣(discount),同时运行销售人员灵活掌握售价(price),在此基础上,对一次购10件以上者,给予9.8折优惠。编程计算当日此商品的总销售款(sum)和平均售价。要求用静态数据成员和静态成员函数。
#include
//统一折扣
const double DISCOUNT = 0.9;
using namespace std;
class Salesman
{
public:
//构造函数
Salesman(int n, int q, double p)
{
this -> m_Num = n;
this -> m_Quantity = q;
this -> m_Price = p;
}
//统一折扣
static double s_Discount;
//给出购买件数计算销售额
static double s_Total(int quantity, int price)
{
double result;
result = quantity * price;
result *= s_Discount;
if(quantity >= 10)
{
result *= 0.98;
}
return result;
}
//函数:获取私有属性值
int GetNum() { return m_Num; }
int GetQuantity() { return m_Quantity; }
double GetPrice() { return m_Price; }
private:
int m_Num; //销货员号
int m_Quantity; //销货件数
double m_Price; //销货单价
};
//静态变量只能在类体外初始化
double Salesman :: s_Discount = DISCOUNT;
int main()
{
Salesman* s[3] =
{
new Salesman(101, 5, 23.5),
new Salesman(102, 12, 24.56),
new Salesman(103, 100, 21.5)
};
double sum =
s[0] -> s_Total(s[0] -> GetQuantity(), s[0] ->GetPrice()) +
s[1] -> s_Total(s[1] -> GetQuantity(), s[1] ->GetPrice()) +
s[2] -> s_Total(s[2] -> GetQuantity(), s[2] ->GetPrice());
cout << "总销售款:" << sum << endl;
double average = sum /
(s[0] -> GetQuantity() + s[1] -> GetQuantity() + s[2] -> GetQuantity());
cout << "每件商品平均售价:" << average << endl;
return 0;
}
9.10)修改例9.13程序
#include
using namespace std;
class Date;
class Time;
class Date
{
public:
Date(int m, int d, int y)
{
this -> m_Month = m;
this -> m_Day = d;
this -> m_Year = y;
}
friend void Display(Date &d, Time &t);
private:
int m_Month;
int m_Day;
int m_Year;
}
g_d = Date(2014, 9, 15);
class Time
{
public:
Time(int h, int m, int s)
{
this -> m_Hour = h;
this -> m_Minute = m;
this -> m_Second = s;
}
friend void Display(Date &d, Time &t);
private:
int m_Hour;
int m_Minute;
int m_Second;
}
g_t = Time(14, 53, 55);
//显示日期和时间
void Display(Date &d, Time &t)
{
cout << g_d.m_Month << "/" << g_d.m_Day << "/" << g_d.m_Year << endl;
cout << g_t.m_Hour << ":" << g_t.m_Minute << ":" << g_t.m_Second << endl;
}
int main()
{
Display(g_d, g_t);
return 0;
}
9.11)修改例9.13程序
#include
using namespace std;
class Date;
class Time;
class Date
{
public:
Date(int m, int d, int y)
{
this -> m_Month = m;
this -> m_Day = d;
this -> m_Year = y;
}
friend class Time; //友元类
private:
int m_Month;
int m_Day;
int m_Year;
}
g_d = Date(2014, 9, 15);
class Time
{
public:
//构造函数
Time(int h, int m, int s)
{
this -> m_Hour = h;
this -> m_Minute = m;
this -> m_Second = s;
}
//展示日期和时间
void Display(Date &d)
{
cout << d.m_Month << "/" << d.m_Day << "/" << d.m_Year << endl;
cout << m_Hour << ":" << m_Minute << ":" << m_Second << endl;
}
private:
int m_Hour;
int m_Minute;
int m_Second;
}
g_t = Time(14, 53, 55);
int main()
{
g_t.Display(g_d);
return 0;
}
9.12)修改例9.14程序
#include
using namespace std;
template
class Compare
{
public:
//构造函数
Compare(numtype a, numtype b)
{
x = a;
y = b;
}
numtype GetMax();
numtype GetMin();
private:
numtype x, y;
};
//找出x与y中的最大者
template
numtype Compare :: GetMax()
{
return x > y ? x : y;
}
//找出x与y中的最小者
template
numtype Compare :: GetMin()
{
return x < y ? x : y;
}
int main()
{
Compare cmp1(3, 7);
cout << "Max:" << cmp1.GetMax() << endl;
cout << "Min:" << cmp1.GetMin() << endl;
Compare cmp2(45.78, 93.6);
cout << "Max:" << cmp2.GetMax() << endl;
cout << "Min:" << cmp2.GetMin() << endl;
Compare cmp3('a', 'A');
cout << "Max:" << cmp3.GetMax() << endl;
cout << "Min:" << cmp3.GetMin() << endl;
return 0;
}
END