C++期末考试复习题库

选择题

  1. C++源程序文件的缺省扩展名为( A )。
`A. cpp        B. exe       C. obj       D. lik`
  1. 用new运算符创建一个含10个元素的一维整型数组的正确语句是( C )。
 A. int *p=new a[10];      B. int *p=new float[10];
        C. int *p=new int[10];    D. int *p=new int[10]={1,2,3,4,5}

  1. 下列的符号常量定义中,错误的定义是(A)。
A、 const M=10;           B、  const char ch; 
        C、 const int M=20;       D、 const bool mark=true;

  1. 关于封装,下列说法中不正确的是( D )。
A. 通过封装,对象的全部属性和操作结合在一起,形成一个整体
    B. 通过封装,一个对象的实现细节被尽可能地隐藏起来(不可见)
    C. 通过封装,每个对象都成为相对独立的实体
    D. 通过封装,对象的属性都是不可见的

  1. 函数重载是指( A )。
A. 两个或两个以上的函数取相同的函数名,但形参的个数或类型不同
    B. 两个以上的函数取相同的名字和具有相同的参数个数,但形参的类型可以不同
    C. 两个以上的函数名字不同,但形参的个数或类型相同
    D. 两个以上的函数取相同的函数名,并且函数的返回类型相同

  1. 为了提高程序的运行速度,可将不太复杂的功能用函数实现,此函数应选择( B )。
  A. 重载函数  B. 内联函数  C.递归函数  D.函数模板
  1. 假定AA为一个类,a为该类公有的数据成员,x为该类的一个对象,则访问x对象中数据成员a的格式为( D )。
 A. x(a)      B. x[a]      C. x->a      D. x.a
  1. 一个类的构造函数通常被定义为该类的( A )成员。
 A. 公有      B. 保护      C. 私有      D. 友元
  1. 类的析构函数可以带有( A )个参数。
 A. 0      B. 1      C. 2      D. 任意
  1. 引入友元的主要目的是为了( C )。
A. 增强数据安全性             B. 提高程序的可靠性
   C. 提高程序的效率和灵活性     D. 保证类的封装性

  1. 派生类的成员函数可以直接访问基类的( B )成员。
 A. 所有     B. 公有和保护     C. 保护和私有    D. 私有
  1. 在定义一个派生类时,若不使用保留字显式地规定采用何种继承方式,则默认为( A)方式。
  A. 私有继承  B. 非私有继承  C. 保护继承    D. 公有继承

在定义一个派生类时,,若是struct,则若不使用保留字显式地规定采用何种继承方式,则默认为公有继承方式。公有继承是最常用的继承方式,它将基类的公有成员和保护成员都继承到派生类中,并且基类的私有成员不能被派生类直接访问。如果需要使用其他继承方式,如私有继承或保护继承,需要使用相应的保留字来显式地指定。class默认是私有。

  1. C++中的虚基类机制可以保证:(D )。
A. 限定基类只通过一条路径派生出派生类
   B. 允许基类通过多条路径派生出派生类,派生类也就能多次继承该基类
   C. 当一个类多次间接从基类派生以后,派生类对象能保留多份间接基类的成员
   D. 当一个类多次间接从基类派生以后,其基类只被一次继承


C++中的虚基类机制可以保证限定基类只通过一条路径派生出派生类。虚基类是指在多重继承中,被派生类的多个基类中,有一个或多个是共享同一个基类的子对象。为了避免虚基类在派生类中出现多次,C++引入了虚基类的概念。虚基类是通过在基类声明中使用关键字virtual来定义的。当一个类从虚基类派生时,虚基类的子对象只会在派生类中出现一次,从而避免了多次继承的问题。虚基类机制可以保证派生类只通过一条路径派生出基类,从而避免了多次继承的问题。

  1. 面向对象方法的多态性是指( C )。
   A. 一个类可以派生出多个特殊类
   B. 一个对象在不同的运行环境中可以有不同的变体
   C. 针对一消息,不同的对象可以以适合自身的方式加以响应
   D. 一个对象可以是由多个其他对象组合而成的

  1. 如果表达式a+b中的“+”是作为成员函数重载的运算符,若采用运算符函数调用格式,则可表示为( A )。
A. a.operator+(b)  B. b.operator+(a)    C. operator+(a,b)  D. operator(a+b)
  1. 以下设置默认值的函数原型声明中错误的是( C )。
A.int add(int x, int y, int z=5);              B.int add(int x, int y=4, int z=5);
C.int add(int x, int y=4, int z);              D.int add(int x=3, int y=4, int z=5);

  1. 下列运算符中,( C )运算符在C++语言中不能重载。
A.+=		B.[ ]		C.::	  D.new
  1. 系统在调用重载函数时往往根据一些条件确定哪个重载函数被调用,在下列选项中,不能作为依据的是( D )。
A.参数的个数                       B.参数的类型    
C.参数的顺序                       D.函数的返回类型

系统在调用重载函数时往往根据参数的个数、参数的类型、参数的顺序和函数的返回类型等条件来确定哪个重载函数被调用。在下列选项中,不能作为依据的是函数的返回类型。函数的返回类型不会影响系统调用重载函数的选择,因为函数的返回类型只是函数的一个属性,不会影响函数的参数列表。而参数的个数、参数的类型和参数的顺序都会影响函数的参数列表,从而影响系统调用重载函数的选择。因此,系统在调用重载函数时往往根据参数的个数、参数的类型和参数的顺序来确定哪个重载函数被调用。

  1. 友元的作用(A )
A.提高程序的运行效率		B.加强类的封装性
C.实现数据的隐藏性			D.增加成员函数的种类

  1. 模板的作用是( D )。
A.提高程序的运行效率		B.加强类的封装性
C.实现数据的隐藏性		D.提高代码可重用性

  1. 基类中的公有成员经过保护派生后,它在派生类中的访问属性是( B )。
A.public	B.protected	C.private	D.不可以被继承
  1. 下列哪个不是构造函数的特征( D )。
A.构造函数名与类名同名			B.构造函数可以重载		
C.构造函数可以设置默认参数		D.构造函数必须指定类型说明

  1. 下列关于静态数据成员的特性中,( D )是错误的。
A.说明静态数据成员时前面要加修饰符static			
B.静态数据成员要在类体外进行初始化		
C.可以使用类名加作用域运算符访问静态数据成员		
D.静态数据成员不是该类所有对象共享的

  1. 关于参数缺省值的设置,正确的是( C)。
A.不允许设置参数的默认值			
B.设置参数默认值只能在定义函数时设置		
C.设置参数默认值时,应该先设置右边的再设置左边的		
D.设置参数默认值时,应该全部参数都设置	
  1. 通过函数实现一种简单功能,并且需要加快执行速度,选用( A )。
A.内联函数		B.重载函数		C.递归调用		D.嵌套调用
  1. 函数的整型参数使用引用方式,为了不改变参数的内容,参数引用的格式应该是(C )。
A.const int  func(int & ) 	B.int  func(int & ) const
C.int  func(const int & ) 	D.int  func(const int * ) const
  1. 关于抽象类,下列说明正确的是( C )。
A.抽象类可以创建对象			B.抽象类中只能有一个纯虚函数
C.抽象类中可以有非纯虚函数	D.抽象类不可派生出抽象类
  1. 下列说明中,
    const char *ptr=”Nanjing”;
    ptr应该是( C )。
A.指向字符常量的指针		B.指向字符的常量指针
C.指向字符串常量的指针		D.指向字符串的常量指针
  1. 多继承派生类构造函数构造对象时,最先被调用的是( B )。
A.派生类构造函数			B.虚基类的构造函数
C.非虚基类的构造函数		D.派生类子对象的构造函数
  1. 下列关于对象数组的描述中,(D )是错误的。
A.对象数组的下标是从0开始的
B.对象数组的数组名是一个常量指针
C.对象数组的每个元素是同一个类的对象
D.对象数组只能赋初值,而不能在定义后赋值

填空题

在这里插入图片描述

读程序

  1. 下面程序的输出结果是?
#include <iostream> 
using namespace std; 
int main()
{
	char *str="abbcabdced";
	int c1=0,c2=0,c3=0,c4=0;
	for(int i=0;str[i];i++)
	    switch(str[i])
	    {
	    	case 'a':c1++;break;
	    	case 'b':c2++;break;
	    	case 'c':c3++;break;
	    	default:c4++;
		}
	cout<<c1<<","<<c2<<","<<c3<<","<<c4<<endl;
	return 0;
 }

结果:2,3,2,3

  1. 下面程序的输出结果是?
#include<iostream>
using namespace std;
class B
{
	int x, y;
public:
	B()
	{   x = y = 0;
		cout << "con1\t";  }
	B(int i)
	{   x = i; 
		y = 0;
		cout << "con2\t";  }
	B(int i, int j)
	{   x = i;
		y = j;
		cout << "con3\t";  }
	~B()
	{   cout << "Des\t";  }
};
int main()
{
	B *ptr;
	ptr = new B[3];
	ptr[0] = B();
	ptr[1] = B(1);
	ptr[2] = B(2,3);
	delete[] ptr;
	return 0;
}

结果:con1 con1 con1 con1 Des con2 Des con3 Des Des Des Des

  1. 下面程序的输出结果是?
include<iostream>
using namespace std;
class Circle
{
	const double PI;
	double r;
public:
	Circle(double rr):PI(3.14)
	{    r=rr;    }
	double Area()const
	{   return PI*r*r;   }
};
int main()
{
	Circle c1(1);
	const Circle c2(2);
	cout<<c1.Area()<<endl;
	cout<<c2.Area()<<endl;
	return 0;
}

3.14
12.56

  1. 下面程序的输出结果是?
#include <iostream>
using namespace std;
class B
{
public:
	B() { cout << "CB"; }
	~B() { cout << "DB"; }
};
class D :public B
{
public:
	D() { cout << "CD"; }
	~D() { cout << "DD"; }
};
int main()
{
	D *p = new D; 
	cout << "HW"; 
	delete p;
 	return 0;
}

结果:CBCDHWDDDB

执行销毁对象指针后再调用析构函数。

  1. 下面程序的输出结果是?
#include <iostream> 
using namespace std; 
class A
{
	public:
		virtual void fun()
		{    cout<<"fun in calss A"<<endl;   }
};
class B
{
	public:
		virtual void fun()
		{   cout<<"fun in calss B"<<endl;   }
};
class C:public A,public B
{
	public:
		void fun()
		{   cout<<"fun in calss C"<<endl;   }
};
int main()
{
	C c;
	A &p1=c;
	B &p2=c;
	C &p3=c;
	p1.fun();
	p2.fun();
	p3.fun();
	return 0;
 }

输出"fun in class C"。而p3本身就是C类型的引用,因此调用p3的fun函数也会输出"fun in class C"。
因此,这段代码的输出结果为:
fun in class C
fun in class C
fun in class C

  1. 下面程序的输出结果是?
#include <iostream>
using namespace std;
void fun(int x,int &y)
{
	x+=y;
	y+=x;
}

int main()
{
	int x=3,y=1;
	fun(x,y);
	fun(y,x);
	cout<<"x="<<x<<",y="<<y<<endl;
	return 0;
}

结果:x=11,y=5

  1. 下面程序的输出结果是?
#include <iostream>
using namespace std;
class C
{ public:
    C(){cout<<"CC";}
    ~C(){cout<<"DC";}
};
int main()
{   C c; cout << "HW"; return 0;}

结果:CCHWDC

  1. 下面程序的输出结果是?
#include <iostream>
using namespace std;
class S
{
    int i;
    static int k;
public:
    S();
    void disp();
};
S::S()
{
    i=0;k++;
}
void S::disp()
{
    cout<<"i="<<i<<",k="<<k<<endl;
}
int S::k=0;
int main()
{
    S a,b;
    a.disp();
    b.disp();
    return 0;
}

i=0,k=2
i=0,k=2

  1. 分析下列程序的访问权限(省略了具体成员函数的实现)(针对具体每个回答“可以”或“不可以”)
#include<iostream.h>
class A
{
public:
		void f1( );
		A(int i, int j ) { i1=i; j1=j;}
protected:
		int j1;
private:
		int i1;
};
class B : protected A
{
public:
		void f2( );
		B(int i,int j, int k ,int l):A(i, j) { i2=k; j2=l;}
protected:
		int j2;
private:
		int i2;
};

(1) 派生类B中成员函数f2()能否访问基类A中的成员 f1( )、i1 和 j1 ?能否访问B中的成员 j2 和 i2 ?

(2) 派生类B的对象b能否访问基类A中的成员 f1( )、i1 和 j1 ? 能否访问B中的成员 j2 和 i2 ?

解答:

(1) 可以,不可以,可以,可以,可以。
(2) 不可以,不可以,不可以,不可以,不可以。

编程题

一、

1.定义一个时间类Time,有3个私有成员变量Hour,Minute,Second,定义构造函数、析构函数以及用于改变、获取、输出时间信息的公有函数,主函数中定义时间对象,并通过调用各种成员函数完成时间的设定、改变、获取、输出等功能。

/*
	1.定义一个时间类Time,有3个私有成员变量Hour,
	Minute,Second,定义构造函数、析构函数以及用于改变、获
	取、输出时间信息的公有函数,主函数中定义时间对象,并通过
	调用各种成员函数完成时间的设定、改变、获取、输出等功能。
*/
#include <iostream>
using namespace std;
class Time{
	private:  
		 int Hour;
		 int Minute;
		 int Second;
	public:
		Time();
		Time(int hour,int minute,int second);
		~Time();
		void SetTime(int hour,int minute,int second);
		int GetHour();	
		int GetMinute();	
		int GetSecond();	
		void Print();
};
Time::Time(){
};
Time::~Time(){
};
Time::Time(int hour,int minute,int second){
	Hour=hour;
	Minute=minute;
	Second=second;
}
void Time::SetTime(int hour,int minute,int second){
	Hour=hour;
	Minute=minute;
	Second=second;
}
int Time::GetHour(){
	return Hour;
}
int Time::GetMinute(){
	return Minute;
}
int Time::GetSecond(){
	return Second;
}
void Time::Print(){
	cout<<Hour<<":" <<Minute<<":"<<Second<<endl;
}
int main(){
	Time a(5,40,30);
	cout<<"before : " ; 
	a.Print();
	cout<<"after :";
	a.SetTime(11,11,11);
	a.Print();
	Time b(10,30,20);
	cout<<"b->hour:"<<b.GetHour()<<endl;
	cout<<"b->minute:"<< b.GetMinute()<<endl;
	cout<<"b->second:"<<b.GetSecond()<<endl;
	return 0;
}

二、

题目要求:

定义Point类,在该类中包含如下内容:
1)私有数据成员:
   double x; //x轴坐标
   double y; //y轴坐标
2)公有成员函数:
   构造函数  //默认值为原点(0.0,0.0)
   void Print();  // 输出坐标点
	Point operator++();  //成员函数重载前置“++”
   Point operator-(const Point &a);  //成员函数重载运算符“-”
	friend Point operator+(const Point &a,const Point &b); //友元函数重载“+”
要求:实现Point类的定义以及成员函数的实现,并在主函数中定义对象进行测试。
>1、已知坐标点Point类定义如下:
class Point
{
	double x,y;
public:
	Point(double a=0,double b=0);//构造函数
friend bool operator>(Point &A,Point &B);//若A点到原点距离大于B点到原点
//距离,则为真;否则为假
	friend double GetLength(Point &A,Point &B);//计算点与点之间的距离
};
试编程实现类中带注释语句的函数,每个5分。

程序代码:

#include <iostream>
#include <cmath>
using namespace std;
class Point{
	private:
		double x; //x轴坐标
	    double y; //y轴坐标
	public:
		Point();
	 	Point(double a=0,double b=0);//构造函数
	 	//友元函数的特点:函数可以直接访问这个类的对象的私有成员
		friend bool operator>(Point &A,Point &B);//若A点到原点距离大于B点到原点
	//距离,则为真;否则为假
		friend double GetLength(Point &A,Point &B);//计算点与点之间的距离
};
Point::Point(){}
Point::Point(double a,double b){
	x=a;
	y=b;	
}
double GetLength(Point &A,Point &B){
	double len=0;
	int d1,d2;
	d1=A.x*A.x+A.y*A.y;
	d2=B.x*B.x+B.y*B.y;
	if(d1>d2){
		len=sqrt((A.x-B.x)*(A.x-B.x)+(A.y*-B.y)*(A.y*-B.y));
	}else{
		len=sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y));
	}
	return len;
}
bool operator > (Point &A,Point &B){
	int d1,d2;
	d1=A.x*A.x+A.y*A.y;
	d2=B.x*B.x+B.y*B.y;
	return d1-d2 > 0 ? true :false;
}
int main(){
	Point A(1,1);
	Point B(2,2);
	bool c=A>B;
	cout<<"A点到原点的距离是否大于B点到原点的距离:";
	if(A>B){
		cout<<"true"<<endl;
	}else{
		cout<<"false"<<endl;
	}
	cout<<"A、B两点之间的距离:";
	cout<<GetLength(A,B)<<endl;
	return 0;
}

补充题目:

(一到九是教材上的题目)

1. 当从键盘上输入“23.56 10 90 <回车>”时,写出下面程序的运行结果。
#include <iostream>
using namespace std;
int main(){
	int a,b,c;
	char ch;
	cin>>a>>ch>>b>>c;
	cout<<a<<endl<<ch<<endl<<b<<endl<<c;
	return 0;
}

23
.
56
10

2. 写出下面的运行结果。
#include <iostream>
using namespace std;
int i=0;
int main(){
	int i=5;
	{
		int i=7;
		cout<<"::i="<< ::i << endl; 
		cout<< "i=" <<i<<endl;
		::i=1;
		cout<<"::i="<<::i<<endl;
	}
	cout<<"i="<<i<<endl;
	cout<<"please input x,y:    ::i= "<<::i<<endl;
	i+=::i;
	::i=100;
	cout<<"i="<<i<<endl;
	cout<<"::i="<<::i<<endl;
	return 0;
}
结果:
```cpp
::i=0
i=7
::i=1
i=5
please input x,y:    ::i= 1
i=6
::i=100
3. 写出下面程序的运行结果。
#include <iostream>
using namespace std;
void fun(int x,int &y){
	x+=y;
	y+=x;
}
int main(){
	int x=5,y=10;
	fun(x,y);
	fun(y,x);
	cout<<"x="<<x<<",y="<<y<<endl;
	return 0;
}

结果:x=35,y=25

4. 定义一个学生类

设计私有数据成员:
年龄 age;
姓名 string name;

公有成员函数:
构造函数:
Student(int m,string n);
Student();
改变数据成员值函数
void SetName(int m,string n);
获取数据成员函数:
int Getage();
string Getname();

结果:

#include <iostream>
using namespace std;
class Student{
	private:
		int age;
		string name;
	public:
		Student();
		Student(int m,string n);
		void SetName(int m,string n);
		int Getage();
		string Getname();
};
Student::Student(){}
Student::Student(int m,string n){
	age=m;
	name=n;
}
void Student::SetName(int m,string n){
	age=m;
	name=n;
}
int Student::Getage(){
	return age;
}
string Student::Getname(){
	return name;
}
int main(){
	Student stu[3]={Student(15,"小红")};
	stu[2].SetName(16,"小明");
	cout<<stu[0].Getage()<<" "<<stu[0].Getname()<<endl;
	cout<<stu[1].Getage()<<" "<<stu[1].Getname()<<endl;
	cout<<stu[2].Getage()<<" "<<stu[2].Getname()<<endl; 
	return 0;
}
5. 设计一个Car类,它的数据成员要能描述一辆汽车的品牌、型号、出场年份和价格,成员函数包括提供合适的途径来访问数据成员,在main()函数中定义类的对象并调用相应成员函数。
#include <iostream>
using namespace std;
class Car{
	private:
		string brand;
		string type;
		int year;
		double price;
	public:
		Car();
		Car(string b,string t,int y,int p);
		string GetBrand();
		string GetType();
		int GetYear();
		double GetPrice(); 
		~Car();
};
Car::Car(){}
Car::Car(string b,string t,int y,int p){
	brand=b;
	type=t;
	year=y;
	price=p;
}
string Car::GetBrand(){
	return brand;
}
string Car::GetType(){
	return type;
}
int Car::GetYear(){
	return year;
}
double Car::GetPrice(){
	return price;
}
Car::~Car(){}
int main(){
	Car c("五菱战神","究极战机",2077,10000000);
	cout<<"品牌:"<<c.GetBrand()<<" 型号:"<<c.GetType()<<" 出场年份:"<<c.GetYear()<<" 价格:"<<c.GetPrice()<<endl; 
	return 0;
}
6. 要求先定义一个Point类,用来产生平面上的点对象。两点决定一条线段,即线段由点构成。因此,Line类使用Point类的对象作为数据成员,然后在Line类的构造函数中求出线段的长度。在main()中定义线段的两个端点,并输出线段的长度。
#include <iostream>
#include <cmath>
using namespace std;
class Point{
	private:
		double X,Y;
	public:
		Point(double a,double b);
		Point(Point &p);
		double GetX();
		double GetY();	
};
class Line{
	private:
		Point A,B;
		double length;
	public:
		Line(Point p1,Point P2);
		double GetLength();
};
Point::Point(double a,double b){
	X=a;
	Y=b;	
}
Point::Point(Point &p){
	X=p.X;
	Y=p.Y;
}
double Point::GetX(){
	return X;
}
double Point::GetY(){
	return Y;
}
Line::Line(Point p1,Point p2):A(p1),B(p2)
{
	length=sqrt((p1.GetX()-p2.GetX())*(p1.GetX()-p2.GetX())+(p1.GetY()-p2.GetY())*(p1.GetY()-p2.GetY()));//因为x,y是私有成员变量,所以不能直接进行访问,要通过成员函数进行间接访问。 
}
double Line::GetLength(){
	return length;
}
int main(){
	Point A(1,1),B(2,2);
	Line line(A,B);
	cout<<line.GetLength()<<endl;
	return 0;
} 
7.

定义一个学生类,有如下基本成员。
(1)私有数据成员:
年龄 int age;
姓名:string name;
(2)公有静态数据成员:
学生人数: static int count;
公有成员函数:
构造函数,带参数的构造函数 Student(int m,string n);
不带参数的构造函数 Student();
析构函数:~Student();
输出函数:void Print() const;

#include <iostream>
#include <cmath>
using namespace std;
class Student{
	private:
		int age;
		string name;
	public:
		static int count;
		Student(int m,string n);
		Student();
		~Student();
		void Print() const;
};
int Student::count=0;
Student::Student(){
	age=0;
	name="NoName";
	count++;
}
Student::Student(int m,string n){
	age=m;
	name=n;
	count++;
}
Student::~Student(){
	count--;
}
void Student::Print()const{
	cout<<count<<endl;
	cout<<"Name="<<name<<" ,age="<<age<<endl;
}
int main(){
	cout<<"count="<<Student::count<<endl;
	Student s1,*p=new Student(18,"小红");
	s1.Print();
	p->Print();
	delete p;
	s1.Print();
	Student Stu[4];//创建四个空对象。
	cout<<"count="<<Student::count<<endl;
	return 0;
}
8. 定义一个表示三维空间坐标点的类,并重载下列运算符,主函数定义类对象并调用重载的运算符。

(1)插入运算符<< :按(x,y,z)格式输出该点坐标(坐标为整型)。
(2)关系运算符> : 如果A点到原点(0,0,0)的距离大于B点原点的距离,则A>B为真,否则为假。

#include <iostream>
using namespace std;
class Point{
	private:
		int x,y,z;
	public:
		Point(int a,int b,int c);
		friend void operator > (Point &p1,Point &p2);
		friend ostream & operator << (ostream & out,const Point &p);
};
Point::Point(int a,int b,int c){
	x=a;
	y=b;
	z=c;
}
void operator > (Point &p1,Point &p2)
{
	int d1,d2;
	d1=(p1.x)*(p1.x)+(p1.y)*(p1.y);
	d2=(p2.x)*(p2.x)+(p2.y)*(p2.y);
	if(d1>d2){
		cout<<"true"<<endl;
	}else{
		cout<<"false"<<endl;
	}
}
ostream & operator << (ostream & out,const Point &p){
	out<<"("<<p.x<<","<<p.y<<","<<p.z<<")"<<endl;
}
int main(){
	Point A(1,1,1);
	Point B(2,2,2);
	cout<<"A"<<A;
	cout<<"B"<<B;
	cout<<"The distance between point A greater than the distance between point B ? "<<endl;
	cout<<"answer:";
	A>B;
	return 0;
}
9. 设计一个矩阵类,要求在矩阵类中重载运算符加(+)、减(-)、乘(*)、赋值(=)和加赋值(+=),主函数定义类对象并调用重载的运算符。
#include <iostream>
using namespace std;
class Matrix{
	public:
		int row,col;
		int *m;
		Matrix();
		Matrix(int r,int c,int *ma);
		Matrix(const Matrix &);
		~Matrix();
		friend Matrix operator + (Matrix &a,Matrix &b);
		friend Matrix operator - (Matrix &a,Matrix &b);
		friend Matrix operator * (Matrix &a,Matrix &b);
		Matrix & operator = (const Matrix &mat);//理解为返回一个矩阵类的引用数据类型 
		Matrix & operator += (Matrix &mat);
		void show();
};
Matrix::Matrix(){
	row=0;
	col=0;
	m=NULL;
}
Matrix::Matrix(int r,int c,int *ma){
	row=r;
	col=c;
	m=new int [row*col];
	for(int i=0;i<row*col;i++){
		*(m+i)=*(ma+i);
	}
}
Matrix::Matrix(const Matrix &mat){
	m=new int  [mat.row*mat.col];
	row=mat.row;
	col=mat.col;
	for(int i=0;i<row;i++){
		for(int j=0;j<col;j++){
			 *(m+i*col+j)=*(mat.m+i*col+j);
		}
	}
}
Matrix::~Matrix(){
	delete [] m;
}
Matrix operator + (Matrix &a,Matrix &b){
	Matrix mat(a);
	if(a.row !=b.row || a.col!=b.col){
		return mat;
	}
	for(int i=0;i<a.row;i++){
		for(int j=0;j<a.col;j++){
			*(mat.m+i*mat.col+j)+=*(b.m+i*b.col+j);
		}
	}
	return mat;
}
Matrix operator - (Matrix &a,Matrix &b){
		Matrix mat(a);
	if(a.row !=b.row || a.col!=b.col){
		return mat;
	}
	for(int i=0;i<a.row;i++){
		for(int j=0;j<a.col;j++){
			*(mat.m+i*mat.col+j)-=*(b.m+i*b.col+j);
		}
	}
	return mat;
}
Matrix operator * (Matrix &a,Matrix &b){
	Matrix temp;
	if(a.col!=b.row) return a;
	temp.m=new int[a.row*b.col];
	temp.row=a.row;
	temp.col=b.col;
	for(int i=0;i<temp.row;i++){
		for(int j=0;j<temp.col;j++){
			*(temp.m+i*temp.col+j)=0;//进行初始化,防止系统自动生成随机数。 
			for(int k=0;k<a.col;k++){
				*(temp.m+i*temp.col+j)=*(temp.m+i*temp.col+j)+ *(a.m+i*a.col+k)**(b.m+k*b.col+j);
				}
		}
	}
	return temp;
}
Matrix& Matrix::operator= (const Matrix &mat){
	delete [] m ;
	m=new int[mat.row*mat.col];
	row=mat.row;
	col=mat.col;
	for(int i=0;i<row;i++){
		for(int j=0;j<col;j++){
			 *(m+i*col+j)=*(mat.m+i*mat.col+j);
		}
	}
	return *this;
}
Matrix& Matrix::operator+= (Matrix &mat){
	if(col!=mat.col || row!=mat.row) return *this; 
	for(int i=0;i<row;i++){
		for(int j=0;j<col;j++){
			*(m+i*col+j)=*(m+i*col+j)+*(mat.m+i*mat.col+j);
		}
	}
	return *this;
}
void Matrix::show(){
	for(int i=0;i<row;i++){
		cout<<endl;
		for(int j=0;j<col;j++){
			cout<<" "<<*(m+i*col+j);
		}
	}
	cout<<endl;
	cout<<endl;
}
int main(){
	int a[3*3]={1,2,3,4,5,6,7,8,9};
	int b[3*3]={9,8,7,6,5,4,3,2,1};
	Matrix am(3,3,a),bm(3,3,b),cm;
	cout<<"a 矩阵:";
	am.show();
	cout<<"b 矩阵:";
	bm.show();
	cout<<"a+b:";
	cm=am+bm;
	cm.show();
	cout<<"a-b:";
	cm=am-bm;
	cm.show();
	cout<<"a*b:";
	cm=am*bm;
	cm.show();
	cout<<"a+=b:";
	am+=bm;
	am.show();
	return 0;
} 
10

已知一个复数类设计如下:

class comp
{
	double real,imag;
public:
	comp(double r=0,double i=0);
	friend comp operator+(const comp &s,const comp &t);
	friend ostream &operator<<(ostream &out,const comp &t);	
};

试实现类中的函数并在main函数中进行测试。

#include <iostream>
using namespace std;
class comp
{
	private:
		double real,imag;
	public:
		comp(double r=0,double i=0);
		friend comp operator+(const comp &s,const comp &t);
		friend ostream &operator<<(ostream &out,const comp &t);	
};
comp::comp(double r,double i){
	real=i;
	imag=i;
}
comp operator +(const  comp  &s,const comp &t){
	comp tem;
	tem.real=s.real+t.real;
	tem.imag=s.imag+t.imag;
	return tem;
}
ostream & operator << (ostream &out,const comp &t){
	out<<t.real<<" + "<<t.imag<<"i"<<endl;
}
int main(){
	comp a(1,1),b(2,2),c;
	c=a+b;
	cout<<"a="<<a;
	cout<<"b="<<b;
	cout<<"a+b= "<<c;
	return 0;
}
11

已知一个复数类设计如下:

class comp
{
	double real,imag;
public:
	comp(double r=0,double i=0);
	//friend bool operator>(const comp &s,const comp &t);//比较到原点的距离,该处题目有问题。
	//换成
	friend bool operator >(const comp &t,const comp &i);
	friend ostream &operator<<(ostream &out,const comp &t);	
};

试实现类中的函数并在main函数中进行测试。

#include <iostream>
using namespace std;
class comp
{
	private:
		double real,imag;
	public:
		comp(double r=0,double i=0);
		friend bool operator>(const comp &t,const comp &i);//比较到原点的距离
		friend ostream &operator<<(ostream &out,const comp &t);	
};

comp::comp(double r,double i){
	real=i;
	imag=i;
}
bool operator>(const comp &t,const comp &i){
	double d1,d2;
	d1=t.real*t.real+t.imag*t.imag;
	d2=i.real*i.real+i.imag*i.imag;
	return d1>d2 ? true : false;
}
ostream & operator << (ostream &out,const comp &t){
	out<<t.real<<" + "<<t.imag<<"i"<<endl;
}
int main(){
	comp a(1,1),b(2,2);
	cout<<a;
	cout<<b;
	if(a>b){
		cout<<"the distance of a to zero bigger than b"<<endl;
	}else{
		cout<<"the distance of b to zero bigger than a"<<endl;
	}
	return 0;
}
12.定义一个学生类stu,包含私有成员 年龄和姓名,接口有:构造函数,修改年龄的函数(年龄增加一岁)。在main函数中对stu类进行测试。

#include <iostream>
using namespace std;
class stu{
	private:
		int age;
		string name;
	public:
		stu();
		stu(int a,string n);
		~stu();
		void changeAge();
		void show();
};
stu::stu(){}
stu::stu(int a,string n){
	age=a;
	name=n;
}
stu::~stu(){}
void stu::changeAge(){
	 age++;
}
void stu::show(){
	cout<<age<<" "<<name<<endl;
}
int  main(){
	stu s(18,"张三");
	cout<<"before: ";
	s.show();
	s.changeAge();
	cout<<"after: ";
	s.show();
	return 0;
}

13设置一个日期类Date,包含私有成员:y,m,d(分别表示年月日),公有成员:构造函数,修改日期函数在原有日期基础上增加一天),打印函数;并在main函数中进行测试上述功能。
#include <iostream>
using namespace std;
class Date{
	private:
		int y;
		int m;
		int d;
	public:
		Date();
		Date(int a,int b,int c): y(a), m(b), d(c){};
		void changeDay();
		void show();
};
void Date::changeDay(){
	d++;
	if(y%4==0 && y%100!=0){
		if(m==2){
			d > 29 ? d=1,++m : ++d;
		}
		else if(m==4||m==6||m==9||m==11){
			d > 30 ? d=1,++m : ++d;
		}else{
			d > 31 ? d=1,++m : ++d;
		}
	}else{
		if(m==2){
			d > 28 ? d=1,++m : ++d;
		}
		else if(m==4||m==6||m==9||m==11){
			d > 30 ? d=1,++m : ++d;
		}else{
			d > 31 ? d=1,++m : ++d;
		}
	}
	if(m>12) m=1,y=y+1;
}
void Date::show(){
	cout<<y<<"年"<<m<<"月"<<d<<"日"<<endl; 
}
int main(){
	Date d(2024,2,29);
	d.show();
	d.changeDay();
	d.show();
	return 0;
}
14.设置一个日期类Date,包含私有成员:y,m,d(分别表示年月日),公有成员:构造函数,拷贝构造函数(在原有日期基础上增加一天),打印函数;并在main函数中进行测试上述功能。

#include <iostream>
using namespace std;
class Date{
	private:
		int y;
		int m;
		int d;
	public:
		Date();
		Date(int a,int b,int c): y(a), m(b), d(c){};
		Date(const Date &D);//深层拷贝
		void show();
		int gety();
		int getm();
		int getd();
		void change();
};
int Date::gety(){
	return y;
};
int Date::getm(){
	return m;
};
int Date::getd(){
	return d;
};
Date::Date(const Date &D):y(D.y),m(D.m),d(D.d){
	change();
}
void Date::change(){
 	d++;
	if(y%4==0 && y%100!=0){
		if(m==2){
			d > 29 ? d=1,++m : ++d;
		}
		else if(m==4||m==6||m==9||m==11){
			d > 30 ? d=1,++m : ++d;
		}else{
			d > 31 ? d=1,++m : ++d;
		}
	}else{
		if(m==2){
			d > 28 ? d=1,++m : ++d;
		}
		else if(m==4||m==6||m==9||m==11){
			d > 30 ? d=1,++m : ++d;
		}else{
			d > 31 ? d=1,++m : ++d;
		}
	}
	if(m>12) m=1,y=y+1;

}
void Date::show(){
	cout<<y<<"年"<<m<<"月"<<d<<"日"<<endl; 
}
int main(){
	Date d1(2024,2,29);
	d1.show();
	Date d2=d1;
	d2.show();
	return 0;
}

15.定义点坐标类Point,包含私有成员x,y,以及成员函数:构造函数,打印函数,前置++重载(用友元函数) 及-(减号)重载,并在main中测试。
#include <iostream>
using namespace std;
class Point{
	private:
		int x;
		int y;
	public:
		Point(){};
		Point(int a,int b):x(a),y(b){};
		friend Point& operator ++(Point &p);
		Point operator -(Point &p);
		void show(); 
};
Point& operator ++(Point &p){
	++p.x;
	++p.y;
	return p;
}
//成员函数
Point& Point::operator ++(){
	++x;
	++y;
	return *this;
}
//后置
Point operator++(Point& p, int) // 后置++重载的友元函数定义
{
    Point temp(p.x, p.y);
    p.x++;
    p.y++;
    return temp;
}
Point Point::operator -(Point &p){
	return Point(x-p.x,y-p.y);
}
void Point::show(){
	cout<<"("<<x<<","<<y<<")"<<endl;
}
int main(){
	Point a(0,0),b(2,2),c;
	++a; 
	c=a-b;
	c.show();
	return 0;
}
  • 25
    点赞
  • 130
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值