C++复习题
- 一、上机题
- 二、课后题
- 2-1 已知:int a=3,b=5;编程计算下列两个代数式的值,并比较它们是否相等。(a+b)^2和a^2+2ab+b^2
- 2-2已知:int x=5;编程求下列代数式的值。f(x)=3x^3+2x^2+5x+2
- 2-3 从键盘上输入两个 double 型数,编程输出其中最小者
- 2-4 华氏温度转换成摄氏温度的计算公式如下:C=(F−32)*5/9其中,C 表示摄氏温度,F 表示华氏温度。从键盘上输入一摄氏温度,编程输出对应的华氏温度。
- 2-5 从键盘上输入 5 个浮点数,输出它们的和以及平均值。
- 2-6 将字符串"12345",逆向输出为"54321"。
- 3-1 求100内的自然数中奇数之和。
- 3-2 求输入两个正整数的最大公约数和最小公倍数
- 3-3 求下列分数序列前 15 项之和。2/1,3/2,5/3,8/5,13/8,…
- 3-4 按下列公式,求 e 的近似值。e=1+1/1!+1/2!+1/3!+…+1/n!
- 3-5.求下列式子之和,假定 n=10。S=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)
- 4-1 已知 4 个字符串,编程输出它们中最小的一个
- 4-2 将一个长度为 n 的字符串,编程实现其逆序输出。
- 4-3 已知一个二维 int 型数组,编程求出它的最小的元素值。
- 4-4 已知字符型指针数组中存放若干个字符串,编程从键盘上修改其中某个字符串。
- 4-5 有问题!!!
- 5-1从键盘上输入 8 个浮点数,编程求出其和以及平均值。要求写出求和以及平均值的函数。
- 5-2 从键盘上输入 8 个整型数,编辑求出它们中间最大的数和最小的数。要求写出求最大数和最小数的函数。
- 5-3 给定某个年、月、日,计算出这一天是属于该年的第几天。要求写出计算闰年的函数和计算日期的函数
- 5-4编写一个程序验证:任何一个充分大的偶数(≥6)总可以表示成两个素数之和。要求编写一个求素数的函数 prine(),它有一个 int 型参数,当参数值为素数时返回 1,否则返回 0
- 6-1.按下列要求编程:
- 6-2编程实现一个简单的计算器。要求从键盘上输入两个浮点数,计算出它们的加、减、乘、除运算的结果
- 6-3编一个关于求多个某门功课总分和平均分的程序。具体要求如下:
- 7-1.按下列要求实现一个栈类的操作。该类名为 Stack,包括如下操作:
- 7-2.按下列要求实现一个有关学生成绩的操作。该类名为 Student。
- 7-3.按如下要求编程验证子对象的有关操作。(1)定义两个类 A 和类 B。 (2)在类 B 中有两个类 A 的对象 one,two。
- 8-1.按下列要求编程:按照右边图中所示的各类的关系,编程输出它们的信息。各类中的数据成员如下:
- 8-2.设计一个程序,一行是信息,下一行画线,所画的线与信息行同长。例如,
- 9-1、编程求圆、圆内接正方形和圈外切正方形的面积和周长。要求使用抽象类
一、上机题
1.求100内的自然数中奇数之和。
// 解法一
#include<iostream>
using namespace std;
int main(){
int sum=0,i;
for(i=1;i<100;i++) {
if(i%2 != 0) sum+=i;
}
cout<<sum<<endl;
}
//2500
//解法2
#include<iostream>
using namespace std;
int main(){
int sum=0,i;
for(i=1;i<100;i=i+2) {
sum+=i;
}
cout<<sum<<endl;
}
2.求输入两个正整数的最大公约数和最小公倍数
#include<iostream>
using namespace std;
int main(){
int m,n,t,r,a,b,y;
cin>>m>>n;
a=m;
b=n;
if(m<n) {
t=m;
m=n;
n=t;
}
while(n!=0){
y=m%n;
m=n;
n=y;
}
r=a*b/m;
cout<<"最大公约数是"<<m<<endl;
cout<<"最小公倍数"<<r<<endl;
}
3.熟悉类的定义和对象的定义。 P126
#include <iostream>
using namespace std;
class Date{
public:
void SetDate(int y,int m,int d){
year=y;month=m;day=d;
}
int IsLeapYear(){
return year%4==0&&year%100!=0||year%400==0;
}
int Print(){
cout<<year<<'/'<<month<<'/'<<day<<endl;
}
private:
int year,month,day;
};
int main(){
Date d1,d2,*pd=&d2;
d1.SetDate(2005,6,24);
pd->SetDate(2000,2,8);
cout<<d1.IsLeapYear()<<','<<d2.IsLeapYear()<<endl;
d1.Print();
d2.Print();
}
/*
0,1
2005/6/24
2000/2/8
*/
4.熟悉类的构造函数和析构函数的使用。 P128
#include <iostream>
using namespace std;
class Date1{
//说明部分
public:
Date1(int,int,int);
Date1(){
cout<<"Default constructor called.\n";
}
~Date1(){
cout <<"Dsetructor called.\t"<<day<<endl;
}
void print();
private:
int year,month,day;
};
//实现部分
Date1::Date1(int y,int m, int d){
year=y;
month=m;
day=d;
cout<<"Constructor callde.\n";
}
void Date1::print(){
cout << year <<'/'<<month<<'/'<<day<<endl;
}
int main(){
static Date1 d1;
Date1 d2(2005,6,25);
cout<<"d1 is";
d1.print();
cout<<"d2 is";
d2.print();
}
/*
Default constructor called.
Constructor callde.
d1 is0/0/0
d2 is2005/6/25
Dsetructor called. 25
Dsetructor called. 0
*/
5. 熟悉函数重载的使用。 P131页
#include <iostream>
using namespace std;
class AB{
public:
AB(int i,int j){
a=i,b=j;
}
AB(int i){
a=i;b=i*i;
}
int Add(int x,int y);
int Add(int x);
int Add();
int aout(){
return a;
}
int bout(){
return b;
}
private:
int a,b;
};
int AB::Add(int x,int y){
a=x;b=y;
return a+b;
}
int AB::Add(int x){
a=b=x;
return a+b;
}
int AB::Add(){
return a+b;
}
int main(){
AB a(5,8),b(7);
cout<<"a="<<a.aout()<<','<<a.bout()<<endl;
cout<<'b='<<b.aout()<<','<<b.bout()<<endl;
int i=a.Add();
int j=a.Add(4);
int k=a.Add(3,9);
cout<<i<<endl<<j<<endl<<k<<endl;
}
/*
a=5,8
b=7,49
13
8
12
*/
7.熟悉对象指针的使用。 P152
#include<iostream>
using namespace std;
class A{
public:
A(int i,int j){
x=i;y=j;
}
A(){
x=y=0;
}
void Setxy(int i,int j){
x=i;y=j;
}
void Copy(A *pa);
void Print(){
cout <<x<<','<<y<<endl;
}
private:
int x,y;
};
void A::Copy(A *pa){
x=pa->x;y=pa->y;
}
void fun(A a1,A *pb){
a1.Setxy(10,15);
pb->Setxy(20,25);
}
int main(){
A a(5,8),b;
b.Copy(&a);
fun(a,&b);
a.Print();
b.Print();
}
/*
5,8
20,25
*/
8.熟悉对象数组的使用。 P158
#include <iostream>
using namespace std;
#include <string.h>
class Student{
char name[20];
long int stuno;
int score;
public :
Student(char name1[]="",long int no=0,int sco=0){
strcpy(name,name1);
stuno =no;
score =sco;
}
void Setscore(int n){
score=n;
}
void Print(){
cout<<stuno<<'\t'<<name<<'\t'<<score<<endl;
}
};
int main(){
Student stu[5]={ Student("Ma",5019001,94),
Student("Hu",5019002,95),
Student("Li",5019003,88)};
stu[3]=Student("Zhu",5019004,85);
stu[4]=Student("Lu",5019005,90);
stu[1].Setscore(98);
for(int i(0);i<5;i++){
stu[i].Print();
}
}
/*
5019001 Ma 94
5019002 Hu 98
5019003 Li 88
5019004 Zhu 85
5019005 Lu 90
*/
某学校教授和讲师的月工资计算办法规定,教授每个月工资固定为4000元。讲师每月工资和讲课学时数有关,计算方法是每学时50元,另加补助1000元。
#include <iostream>
#include <string.h>
using namespace std;
class gongzi{
public:
gongzi(char s[],double i){
strcpy(name,s);
a=i;
}
virtual void gz()=0;
protected:
char name[10];
double a;
};
class jiaoshou :public gongzi{
public:
jiaoshou(char s[],double i):gongzi(s,i){
}
void gz(){
cout<<name;
cout<<"工资为4000"<<endl;
}
};
class jiangshi :public gongzi{
public :
jiangshi (char s[],double i):gongzi (s,i){
}
void gz(){
cout<<name;
cout<<"工资为"<<a*50+1000<<endl;
}
};
int main(){
gongzi *ps;
ps=new jiaoshou("张聪聪",4000);
ps->gz();
ps=new jiangshi("张聪聪",4);
ps->gz();
}
二、课后题
2-1 已知:int a=3,b=5;编程计算下列两个代数式的值,并比较它们是否相等。(a+b)2和a2+2ab+b^2
#include <iostream>
using namespace std;
int main(){
int t,n,a=3,b=5;
t=(a+b)*(a+b);
n=a*a+2*a*b+b*b;
cout<<"t="<<t<<endl;
cout<<"n="<<n<<endl;
cout<<(t==n)<<endl;
}
2-2已知:int x=5;编程求下列代数式的值。f(x)=3x3+2x2+5x+2
#include <iostream>
using namespace std;
int main(){
int x=5,f;
f=3*x*x*x+2*x*x+5*x+2;
cout<<f<<endl;
}
/*
452
*/
2-3 从键盘上输入两个 double 型数,编程输出其中最小者
#include <iostream>
using namespace std;
int main(){
double a,b;
cin>>a>>b;
if(a<=b) cout<<a<<endl;
else cout <<b<<endl;
}
2-4 华氏温度转换成摄氏温度的计算公式如下:C=(F−32)*5/9其中,C 表示摄氏温度,F 表示华氏温度。从键盘上输入一摄氏温度,编程输出对应的华氏温度。
#include <iostream>
using namespace std;
int main(){
double C,F;
cin>>C;
F=9.0/5.0*C+32.0;
cout <<F<<endl;
}
2-5 从键盘上输入 5 个浮点数,输出它们的和以及平均值。
#include <iostream>
using namespace std;
int main(){
double a,b,c,d,e,f,av;
cin>>a>>b>>c>>d>>e;
f=a+b+c+d+e;
av=f/5.0;
cout<<f<<','<<av<<endl;
}
2-6 将字符串"12345",逆向输出为"54321"。
解一:
#include <iostream>
using namespace std;
int main(){
char a[]="12345";
int i=4;
for(i=4;i>=0;i--){
cout<<a[i];
}
}
解二:
#include <iostream.h>
void main()
{
char a[]="12345";
cout<<a[4]<<a[3]<<a[2]<<a[1]<<a[0]<<endl;
}
3-1 求100内的自然数中奇数之和。
// 解法一
#include<iostream>
using namespace std;
int main(){
int sum=0,i;
for(i=1;i<100;i++) {
if(i%2 != 0) sum+=i;
}
cout<<sum<<endl;
}
//2500
//解法2
#include<iostream>
using namespace std;
int main(){
int sum=0,i;
for(i=1;i<100;i=i+2) {
sum+=i;
}
cout<<sum<<endl;
}
3-2 求输入两个正整数的最大公约数和最小公倍数
#include<iostream>
using namespace std;
int main(){
int m,n,t,r,a,b,y;
cin>>m>>n;
a=m;
b=n;
if(m<n) {
t=m;
m=n;
n=t;
}
while(n!=0){
y=m%n;
m=n;
n=y;
}
r=a*b/m;
cout<<"最大公约数是"<<m<<endl;
cout<<"最小公倍数"<<r<<endl;
}
3-3 求下列分数序列前 15 项之和。2/1,3/2,5/3,8/5,13/8,…
#include <iostream>
using namespace std;
int main(){
int a;
double sum=0,i=2,j=1,t;
for(a=1;a<15;a++){
sum+=i/j;
t=i;
i+=j;
j=t;
}
cout<<sum<<endl;
}
3-4 按下列公式,求 e 的近似值。e=1+1/1!+1/2!+1/3!+…+1/n!
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double e=1.0f;
int n, i=1;
long f=1;
cin>>n;
while (i<=n) {
e += 1.0/f;
f*=++i;
}
printf("%.10f", e);
}
3-5.求下列式子之和,假定 n=10。S=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)
解一:不局限于n=10
#include <iostream>
using namespace std;
int main(){
int i, n,S=0,t=0;
cin>>n;
for(i=1;i<=n;i++){
t=t+i;
S=S+t;
}
cout<<S<<endl;
}
#include <iostream.h>
void main()
{
int s(0),s1(0);
for(int i=1;i<=10;i++)
{
s1+=i;
s+=s1;
}
cout<<s<<endl;
}4-1 已知 4 个字符串,编程输出它们中最小的一个
#include <iostream>
#include <string.h>
using namespace std;
void swap(char p1[],char p2[]);
int main(){
char s1[10]="while",s2[10]="else",
s3[10]="default",s4[10]="continue";
if(strcmp(s1,s2)) swap(s1,s2);
if(strcmp(s3,s4)) swap(s3,s4);
if(strcmp(s1,s3)) swap(s1,s3);
cout <<s1<<endl;
return 0;
}
void swap(char p1[],char p2[]){
char p[10];
strcpy(p,p1);
strcpy(p1,p2);
strcpy(p2,p);
}
4-1 已知 4 个字符串,编程输出它们中最小的一个
#include <iostream>
#include <string.h>
using namespace std;
void swap(char p1[],char p2[]);
int main(){
char s1[10]="while",s2[10]="else",
s3[10]="default",s4[10]="continue";
if(strcmp(s1,s2)) swap(s1,s2);
if(strcmp(s3,s4)) swap(s3,s4);
if(strcmp(s1,s3)) swap(s1,s3);
cout <<s1<<endl;
return 0;
}
void swap(char p1[],char p2[]){
char p[10];
strcpy(p,p1);
strcpy(p1,p2);
strcpy(p2,p);
}
4-2 将一个长度为 n 的字符串,编程实现其逆序输出。
#include <iostream>
#include <string.h>
using namespace std;
void inverse (char []);
int main(){
char s[50];
cout<<"输入一个字符串:";
cin>>s;
inverse(s);
cout<<"反序后的字符串是"<<s<<endl;
}
void inverse(char p[]){
int n=strlen(p);
int limit=n/2;
for(int i=0;i<limit;i++){
char t=p[i];
p[i]=p[n-i-1];
p[n-i-1]=t;
}
}
4-3 已知一个二维 int 型数组,编程求出它的最小的元素值。
#include <iostream>
using namespace std;
int main(){
int n[3][2]={1,2,3,5,4,6};
int min=n[0][0];
for(int i=0;i<3;i++)
for(int j=0;j<2;j++)
if(n[i][j]<min)
min=n[i][j];
cout<<min<<endl;
return 0;
}
4-4 已知字符型指针数组中存放若干个字符串,编程从键盘上修改其中某个字符串。
#include <iostream>
#include <string.h>
using namespace std;
char *str[5]={"abc","def","ghi","jkl","pqo"};
char *modify(int,char []);
int main(){
int n;
char s[10];
cout<<"修改第几个字符串";
cin>>n;
cout<<"字符串修改为";
cin>>s;
cout<<"修改后的字符串"<<modify(n,s)<<endl;
cout<<"修改后字符执着数组中的字符串如下\n";
for(int i=0;i<5;i++) cout<<str[i]<<endl;
}
char *modify(int n,char s[]){
return *(str+n-1)=s;
}
4-5 有问题!!!
5-1从键盘上输入 8 个浮点数,编程求出其和以及平均值。要求写出求和以及平均值的函数。
#include <iostream>
using namespace std;
double sum(double b[], int n);
double ave(double a,int n);
int main(){
int n=8;
double a[8],b=0,c;
cout<<"请输入八个浮点数";
for(int i=0;i<8;i++){
cin>>a[i];
}
b=sum(a,n);
c=ave(b,n);
cout<<"sum="<<b<<endl;
cout<<"ave="<<c<<endl;
}
double sum(double b[], int n){
double sum=0;
for(int i=0;i<n;i++){
sum+=b[i];
}
return sum;
}
double ave(double a,int n){
double ave;
ave =a/n;
return ave;
}
5-2 从键盘上输入 8 个整型数,编辑求出它们中间最大的数和最小的数。要求写出求最大数和最小数的函数。
#include <iostream>
using namespace std;
int max(int b[],int n),min(int b[],int n);
int main()
{
int a[8];
cout<<"输入 8 个 int 型数 ;";
for(int i=0;i<8;i++)
cin>>a[i];
cout<<"最大数为 "<<max(a,8)<<','<<"最小数为 "<<min(a,8)<<endl;
}
int max(int b[],int n)
{
int max=b[0];
for(int i=0;i<n;i++)
if(b[i]>max)
max=b[i];
return max;
}
int min(int b[],int n)
{
int min=b[0];
for(int i=0;i<n;i++)
if(b[i]<min)
min=b[i];
return min;
}
5-3 给定某个年、月、日,计算出这一天是属于该年的第几天。要求写出计算闰年的函数和计算日期的函数
#include <iostream>
using namespace std;
int sum_day(int,int ),leap(int);
int main(){
int year,month,day;
cout<<"请输入日期(yyy mm dd)";
cin>>year>>month>>day;
int days=sum_day(month,day);
if(leap(year)&&month>2) days++;
cout<<year<<"年"<<month<<"月"<<day<<"日是这一年的第"<<days<<"天。\n";
}
int sum_day(int month,int day ){
int day_month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
for(int i=1;i<month;i++) day+=day_month[i];
return day;
}
int leap(int year){
int leap=year%4==0&&year%100!=0||year%400==0;
return leap;
}
5-4编写一个程序验证:任何一个充分大的偶数(≥6)总可以表示成两个素数之和。要求编写一个求素数的函数 prine(),它有一个 int 型参数,当参数值为素数时返回 1,否则返回 0
6-1.按下列要求编程:
(1)定义一个描述矩形的类 Rectangle,包括的数据成员有宽(width)和长(length);
(2)计算矩形周长;
(3)计算矩形面积;
(4)改变矩形大小。通过实例验证其正确性
大小。通过实例验证其正确性
#include <iostream>
using namespace std;
class Rectangle{
public:
Rectangle(int a,int b){
width=a;
length=b;
}
int Area(){//面积
return width*length;
}
int Periment(){//周长
return 2*(width+length);
}
int Changesize(int a,int b){
width=a; length=b;
}
void Print();
private:
int width, length;
};
void Rectangle::Print(){
cout<<"AREA="<<Area()<<endl;
cout<<"PERIMENT="<<Periment()<<endl;
}
int main(){
Rectangle r(5,8);
r.Print();
r.Changesize(3,9);
r.Print();
}
6-2编程实现一个简单的计算器。要求从键盘上输入两个浮点数,计算出它们的加、减、乘、除运算的结果
#include <iostream>
using namespace std;
class ASMD{
public:
ASMD(double a,double b ){
x=a;
y=b;}
void add(){
cout<<"x+y="<<x+y<<endl;
}
void jian(){
cout<<"x-y="<<x-y<<endl;
}
void cheng(){
cout<<"x*y="<<x*y<<endl;
}
void chu(){
cout<<"x/y="<<x/y<<endl;
}
void Print();
private:
double x,y;
};
void ASMD::Print(){
add();
jian();
cheng();
chu();
}
int main(){
double x,y;
cin>>x>>y;
ASMD a(x,y);
a.Print();
}
6-3编一个关于求多个某门功课总分和平均分的程序。具体要求如下:
(1)每个学生信息包括姓名和某门功课成绩。
(2)假设 5 个学生。
(3)使用静态成员计算 5 个学生的总成绩和平均分
#include <iostream>
#include <string.h>
using namespace std;
class Student{
public:
Student(char s[],int gr){
strcpy(name,s);
grade=gr;
totalize+=gr;
}
static void Print();
private:
char name[10];
int grade;
static int totalize;
};
int Student::totalize=0;
void Student::Print(){
cout<<"sum="<<totalize<<endl;
cout<<"ave="<<totalize/5 <<endl;
}
int main(){
Student s1("aa",100),s2("bb",100),s3("ll",100),s4("cc",100),s5("zhang",100);
Student::Print();
}
7-1.按下列要求实现一个栈类的操作。该类名为 Stack,包括如下操作:
(1)压栈操作:Push();
(2)弹栈操作:Pop();
(3)获取栈顶元素:Peer();
(4)判栈空操作:IsEmpty();
(5)判栈满操作:IsPull()。
设栈最多可存放 50 个整数。
栈中成员用数组表示。
编写一个程序,定义一个栈类的对象数组来验证该类操作
#include <iostream>
#include <stdlib.h>
using namespace std;
class Stack{
public:
Stack(int i);
~Stack(){
delete[] sta;
}
void Push(int i);
int Pop();
void IsUp11(){
if(tos==length){
cout<<"Stack is fill.\n";
return ;
}
}
int IsEmpty(){
if(tos==0){
cout<<"Stack underflow.\n";
return 0;
}
}
private:
int *sta;
int tos,length;
};
Stack::Stack(int i)
{
sta=new int[i];
if(!sta)
{
cout<<"Can't allocate stack.\n";
abort();
}
tos=0;
length=i;
}
void Stack::Push(int i)
{
sta[tos]=i;
tos++;
}
int Stack::Pop()
{
tos--;
return sta[tos];
}
int main()
{
Stack a(50);
a.Push(1);
a.Push(2);
a.Push(3);
a.Push(4);
a.Push(5);
a.Push(6);
a.Push(7);
a.Push(8);
a.Push(9);
a.Push(10);
cout<<a.Pop()<<',';
cout<<a.Pop()<<',';
cout<<a.Pop()<<',';
cout<<a.Pop()<<',';
cout<<a.Pop()<<',';
cout<<a.Pop()<<',';
cout<<a.Pop()<<',';
cout<<a.Pop()<<',';
cout<<a.Pop()<<',';
cout<<a.Pop()<<','<<endl;
}
7-2.按下列要求实现一个有关学生成绩的操作。该类名为 Student。
(1)每个学生的信息包含有姓名(字符数组)和成绩(int 型)。
(2)共有 5 个学生,用对象数组表示。
(3)计算出 5 个学生中的最高分,并输出姓名及分数。
#include <iostream>
#include <string.h>
using namespace std;
class Student{
public:
Student(char s[],int gr){
strcpy(name,s);
grade=gr;
}
friend void fun();
private:
char name[10];
int grade;
};
Student ss[5]={Student(" 马 力 ",85),
Student(" 王 欣",96),
Student("李明",89),
Student("赵亮",78),
Student("张京",80)};
void fun(){
int k=0,max=ss[0].grade;
for(int i=0;i<5;i++){
if(ss[i].grade>max){
max=ss[i].grade;
k=i;
}
}
cout<<"最高分的学生姓名和成绩如下\n"<<ss[k].name<<','<<ss[k].grade<<endl;
}
int main(){
fun();
}
7-3.按如下要求编程验证子对象的有关操作。(1)定义两个类 A 和类 B。 (2)在类 B 中有两个类 A 的对象 one,two。
验证如下事实:
(1)在类 B 的构造函数中应该包含对两个类 A 的子对象的初始
化项,被放在成员初始化列表中。
(2)在类 B 的默认构造函数中隐含着子对象的初始化项。
(3)在类 B 的析构函数中也隐含着子对象的析构函数。
(4)调用子对象构造函数的顺序。
#include <iostream>
using namespace std;
class A{
public:
A(){
cout<<"In A0.\n";}
A(int i){
a=i;cout<<"In A1."<<a<<endl;
}
~A(){
cout <<"In A2."<<a<<endl;}
int a;
};
class B{
public:
B(){
cout <<"In B0.\n";
}
B(int i,int j,int k):two(j),one(k){
b=i;
cout<<"In B1.\n";
}
void Print(){
cout<<b<<','<<one.a<<','<<two.a<<endl;
}
~B(){
cout<<"In B2.\n";
}
private:
int b;
A one ,two;
};
int main(){
cout<<"构造函数的调用情况:\n";
static B bb0;
B bb(1,2,3);
cout<<"输入对象bb的数据成员值:\n";
bb.Print();
cout <<"析构函数的调用情况:\n";
}
/*
构造函数的调用情况:
In A0.
In A0.
In B0.
In A1.3
In A1.2
In B1.
输入对象bb的数据成员值:
1,3,2
析构函数的调用情况:
In B2.
In A2.2
In A2.3
In B2.
In A2.0
In A2.0
*/
8-1.按下列要求编程:按照右边图中所示的各类的关系,编程输出它们的信息。各类中的数据成员如下:
Person: char *name(姓名),*dept(系别) Student: char *grade(年级)
Teacher: char *lesson(授课名)
Student Teacher(在职读研): char *major(专业方向)
*/
#include <iostream>
#include <string.h>
using namespace std;
class Person{
public:
Person(char *s){
name =new char[strlen(s)+1];
strcpy(name,s);
}
void Print(){
cout<<"姓名:"<<name<<endl;
}
protected:
char *name;
};
class Student: virtual public Person{
public:
Student(char *s,char *t):Person(s){
grad=new char[strlen(t)+1];
strcpy(grad,t);
}
void Print(){
Person::Print();
cout<<"年级:"<<grad<<endl;
}
protected :
char *grad;
};
class Teacher:virtual public Person{
public:
Teacher(char *s,char *t):Person(s){
lesson =new char[strlen(t)+1];
strcpy(lesson,t);
}
void Print(){
Person::Print();
cout<<"授课名:"<<lesson<<endl;
}
protected:
char*lesson;
};
class StudentTeacher:public Student ,public Teacher{
public:
StudentTeacher(char *p,char *q,char *r,char *s):
Student(p,q),Teacher(p,r),Person(p){
Major= new char [strlen(s)+1];
strcpy(Major,s);
}
void Print(){
Student::Print();
cout<<"专业方向:"<<Major<<endl;
}
protected:
char *Major;
};
int main(){
Student stu("马宇","06级");
Teacher tea("王新","高等数学");
StudentTeacher stutea("刘力","07级","数据结构","计算机软件");
stu.Print();
tea.Print();
stutea.Print();
}
8-2.设计一个程序,一行是信息,下一行画线,所画的线与信息行同长。例如,
C++
Programming
*/
#include <iostream>
#include <string.h>
using namespace std;
class Line{
public:
Line(int i){
length=i;
}
void Show(){
for(int i=0;i<length;i++)
cout<<'_';
cout<<endl;
}
private:
int length;
};
class Message{
public:
Message(char *ptr){
msg=new char[strlen(ptr)+1];
strcpy(msg,ptr);
}
~Message(){
delete []msg;
}
void Show(){
int i=0;
cout<<msg<<endl;
}
private:
char *msg;
};
class MsgLine:public Line,public Message{
public:
MsgLine(char *ptr):Message(ptr),Line(strlen(ptr)){
}
void Show(){
Message::Show();
Line::Show();
}
};
int main(){
MsgLine string1("C++");
string1.Show();
MsgLine string2("Progrmming");
string2.Show();
}
/*
C++
___
Progrmming
__________
*/
9-1、编程求圆、圆内接正方形和圈外切正方形的面积和周长。要求使用抽象类
#include <iostream>
using namespace std;
const double PI=3.1415;
class Shape{
public:
Shape (double i){
r=i;
}
virtual void Area()=0;
virtual void Perimeter()=0;
protected:
double r;
};
class Circle:public Shape{
public:
Circle(double i):Shape(i){
}
void Area(){
cout<<"圆的面积是"<<PI*r*r<<endl;
}
void Perimeter(){
cout<<"圆的周长是"<<2*PI*r<<endl;
}
};
class In_Square:public Shape{
public:
In_Square(double i):Shape(i){
}
void Area(){
cout<<"正方形的面积:"<<2*r*r<<endl;
}
void Perimeter(){
cout<<"正方形的周长:"<<4*1.414*r<<endl;
}
};
class Ex_Square :public Shape{
public:
Ex_Square(double i):Shape(i){
}
void Area(){
cout<<"外接正方形的面积:"<<4*r*r<<endl;
}
void Perimeter(){
cout<<"外接正方形的周长:"<<8*r<<endl;
}
};
int main(){
Shape *ps;
ps=new Circle(8);
ps->Area();
ps->Perimeter();
ps=new In_Square(8);
ps->Area();
ps->Perimeter();
ps=new Ex_Square(8);
ps->Area();
ps->Perimeter();
delete ps;
}