大学《C语言程序设计》期末复习资料

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1. 有一个Person类,私有数据成员name、age和sex分别表示人的姓名、年龄和性别。

雇员类Employee是Person的派生类,新增数据成员部门department和薪水salary。

请用C++代码描述这两个类,并用Employee类的成员函数Display实现雇员的姓名、年龄、性别、部门和薪水的输出。

#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
Person(char *s1 = " ", int a = 0, char *s2 = " ")
{
strcpy(name, s1);
age = a;
strcpy(sex, s2);
}
void Display()
{
cout << name << " " << age << " " << sex << " ";
}
private:
char name[8];
int age;
char sex[2];
};
class Employee:public Person
{
public:
Employee(char *s1 = " ", int a = 0, char *s2 = " ", char *s3 = " ", double s = 0):
Person(s1, a, s2)
{
strcpy(department, s3);
salary = s;
}
void Display()
{
Person::Display(); cout << department << " " << salary << endl; }
private:
char department[20];
double salary;
};
void main()
{
Employee ee("张三",28,"男","计算机",9000);
ee.Display();
}
2. c++程序设计,实现长方体的面积和体积计算
(1)长方体的面积计算公式: s=2(a*b+b*c+c*a);
(2)长方体的体积计算公式:v=a*b*c;
#include<iostream>
using namespace std;
class Changfangti
{
private:
double Long;
double Wide;
double High;
public:
Changfangti(double a, double b, double c);
double area();
double volume();
};
Changfangti::Changfangti(double a, double b, double c)
{
Long = a;
Wide = b;
High = c;
}
double Changfangti::area()
{
return 2 * ((Long*Wide) + (Long*High) + (Wide* High));
}
double Changfangti::volume()
{
return (Long*Wide*High);
}
int main()
{
cout << "请输入:" << endl;
double a, b, c;
cin >> a >> b >> c;
Changfangti a1(a,b,c);
cout << "长方体的体积为:" << a1.volume() <<endl;
cout << "长方体的面积为:" << a1.area() << endl;
while (1);
return 0;
}
3. 【例题9-8】定义一个类 Time, 私有数据成员为hour、 minute和sec,声明输入(set_time)/输出(show_time)时间的成员函数和引用变量t;在类外定义函数set_time,和show_time函数,t为引用变量;在主函数中定义Time类的对象t1,调用t1的成员函数set_time输入数据和show_time输出t1的数据。

#include <iostream>
using namespace std;
class Time
{
private:
int hour;
int minute;
int sec;
public:
void set_time(Time &t);
void show_time(Time &t);
};
int main()
{
Time t1;
t1.set_time(t1);
t1.show_time(t1);
函数Time t2;
t2.set_time(t2);
t2.show_time(t2);
return 0;
}
void Time::set_time(Time &t)
{
cout<<"please input time:"<<endl;
cin>>t.hour;
cin>>t.minute;
cin>>t.sec;
}
void Time::show_time(Time &t)
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
4..编写程序,定义一个类模板Carray,模板中有两个成员变量,一个是指针变量,用来指向数组元素所在空间的首地址(数组元素可以是任意的);另一个为数组元素的个数。

定义类的成员函数,用以实现不同类型数组的累加及输出。

#include<iostream>
using namespace std;
template<typename T>
class Carray
{
public:
T add(T ary[],int n)
{
p=ary;
size=n;
T sum=0;
for( int i=0; i<size; i++ )
{
sum += *(p+i);
}
return sum;
}
private:
T *p;
int size;
};
int main()
{
Carray<int> c1;
int i_ary[5];
cout<<"请输入5个整数:"<<endl;
for(int i=0;i<5;i++)
cin>>i_ary[i];
}
cout<<"i_sum="<<c1.add(i_ary,5)<<endl;
Carray<double> c2;
double d_ary[3];
cout<<"请输入3个浮点数:"<<endl;
for(int i=0;i<3;i++)
{
cin>>d_ary[i];
}
cout<<"d_sum="<<c2.add(d_ary,3)<<endl;
return 0;
}
5.编写程序,定义一个类模板CValue ,该模板中可以存放六个值,并且包含一个成员函数。

成员函数的功能是求六个数中的最大值。

要求用户输入任意类型的六个数(int型、double 型等),将最大值输出到屏幕上。

#include<iostream>
using namespace std;
template<typename T,int size=6>
class CValue
{
public:
T max(T ary[])
{
T max_val = 0; //保存最大值的变量
for( int i=0; i<size; i++) //遍历数组
{
if( ary[i] > max_val ) //如果当前元素比当前最大值大
{
max_val = ary[i]; //修改保存最大值变量
}
return max_val; //返回最大值
}
private:
T ary[size];
};
int main()
{
CValue<int> c1;
int i_ary[6];
cout<<"please input six integers:"<<endl;
for(int i=0;i<6;i++)
{
cin>>i_ary[i];
};
cout<<"the i_max="<<c1.max(i_ary)<<endl;
CValue<double> c2;
double d_ary[6];
cout<<"please input six doubles:"<<endl;
for(int i=0;i<6;i++)
{
cin>>d_ary[i];
};
cout<<"the d_max="<<c2.max(d_ary)<<endl;
return 0;
}
7.
#include< iostream >
using namespace std;
template< typename T, int size >
class Sum
{
T add( T (&ary)[size] ) //成员函数定义
{
T sum=0;
for( int i=0; i<size; i++ )
{
sum += ary[i];
}
return sum;
}
};
int main()
{
Sum<int, 5> i; //用int型和整数5产生实例化类模板Sum Sum<double, 3> d; //用double型和整数3产生实例化类模板Sum int i_ary[5]={1, 2, 3, 4, 5};
double d_ary[3]={1.1, 2.2, 3.3};
cout<<"i_sum="<<i.add(i_ary)<<endl; //调用类成员函数,并输出cout<<"d_sum="<<d.add(d_ary)<<endl;
return 0;
}
6. 写一个程序,定义抽象基类Shape,由它派生出5个派生类:Circle、Square、Rectangle、Tarpezoid、Triangle。

用虚函数分别计算几种图形面积,并求它们的和。

要求用基类指针数组,使它的每一个元素指向一个派生类对象。

#include <iostream>
#include <iomanip>
using namespace std;
class Shape
{
public:
Shape(){}
virtual ~Shape(){}
virtual double Area() const{return 0;}
virtual void shapeName() const =0;
};
class Circle: public Shape
{
public:
Circle(double r){radius=r;}
~Circle(){}
virtual double Area() const {return 3.14159*radius*radius;}
virtual void shapeName() const {cout<<"Circle ";}
protected:
double radius;
};
class Square: public Shape
{
public:
Square(double a){x=a;}
~Square(){}
virtual double Area() const {return x*x;}
virtual void shapeName() const {cout<<"Square ";} protected:
double x;
};
class Rectangle: public Shape
{
public:
Rectangle(double a, double b){x=a; y=b;}
~Rectangle(){}
virtual double Area() const {return x*y;}
virtual void shapeName() const {cout<<"Rectangle ";} protected:
double x, y;
};
class Trapezoid: public Shape
{
public:
Trapezoid(double a, double b, double c){x=a; y=b; z=c;} ~Trapezoid(){}
virtual double Area() const{return ((x+y)*z)/2;}
virtual void shapeName() const {cout<<"Trapezoid ";} protected:
double x, y, z;
};
class Triangle: public Shape
{
public:
Triangle(double a, double h){x=a; y=h;}
~Triangle(){}
virtual double Area() const {return (x*y)/2;}
virtual void shapeName() const {cout<<"Triangle ";} protected:
double x, y;
};
int main()
{
Circle circle(5);
Square square(4);
Rectangle rectangle(3, 4);
Trapezoid trapezoid(2.0, 3.0, 4.0);
Triangle triangle(4, 3);
double sum=0;
Shape *pt[5]={&circle, &square, &rectangle, &trapezoid, &triangle};
Shape **p;
for (p=pt; p<pt+5; p++){
(*p)->shapeName();
cout<<"area="<<(*p)->Area()<<endl;
sum+=(*p)->Area();
}
cout<<"Area sum="<<sum<<endl;
system("pause");
return 0;
}。

相关文档
最新文档