栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

4. 构造函数与析构函数)【C++】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

4. 构造函数与析构函数)【C++】

4- 判断题

1-1
C++程序中,类的构造函数名与类名相同。T

1-4
在C++语言中引入内联函数(inline function)的主要目的是降低空间复杂度,即缩短目标代码长度。 F

1-3
形参 int fun(int a=1,int b,int c=2)合法 F

单选题

2-1
下列函数中,( )不能重载。 C
A.成员函数
B.非成员函数
C.析构函数
D.构造函数

2-2
下列对重载函数的描述中,( )是错误的。 A
A.重载函数中不允许使用默认参数
B.重载函数中编译根据参数表进行选择
C.不要使用重载函数来描述毫无相干的函数
D.构造函数重载将会给初始化带来多种方式

2-3
在下面类声明中,关于生成对象不正确的是( )。C

class point
{ public:
int x;
int y;
point(int a,int b) {x=a;y=b;}
};

A.point p(10,2);
B.point *p=new point(1,2);
C.point *p=new point[2];
D.point *p[2]={new point(1,2), new point(3,4)};

2-4
设A为自定义类,现有普通函数int fun(A& x)。则在该函数被调用]时:C
A.将执行复制构造函数来初始化形参x
B.仅在实参为常量时,才会执行复制构造函数以初始化形参x
C.无需初始化形参x
D.仅在该函数为A类的友元函数时,无需初始化形参x

2-5
析构函数可以返回:D
A.指向某个类的指针
B.某个类的对象
C.状态信息表明对象是否被正确地析构
D.不可返回任何值

2-6
建立一个类对象时,系统自动调用 A
A.构造函数
B.析构函数
C.友元函数
D.成员函数

2-7
在下列关键字中,用以说明类中公有成员的是( )。A
A.public
B.private
C.protected
D.friend

2-8
有关类和对象的说法下列不正确的有( )。C
A.对象是类的一个实例
B.任何一个对象只能属于一个具体的类
C.一个类只能有一个对象
D.类与对象和关系与数据类型和变量的关系相似

2-9
下列属于类的析构函数特征的是 A
A.一个类中只能定义一个析构函数
B.析构函数名与类名不同
C.析构函数的定义只能在类体内
D.析构函数可以有一个或多个参数

2-10
下列各类函数中,不是类的成员函数的是 C
A.构造函数
B.析构函数
C.友元函数
D.拷贝构造函数

填空题

4-2
基本概念
类是对象的抽象,而一个对象则是其对应的一个 实例。

4-3
基本概念

在面向对象程序设计方法中,对象是系统中用来描述客观事物的一个实体,它由 在这里插入代码片和可执行的一组操作共同组成。

4-4
A member with the samename as its class is called a constructor. A constructor declaration specifies an argument list (exactly as for a function) but has no return type. The name of a class cannot be used for an ordinary member function, data member, member type, etc., within the class.

4-5
A constructor’s job is to initialize an object of its class.

程序填空题

5-1
阅读下列程序并填空:

#include 
#include 
using namespace std; 
class Book{
private:
  char *title; //书名
  char *author; //作者
  int sales;    //销售量
 public:
  Book();   //默认构造函数;
  Book(char *a,char *b,int c); //构造函数
   void print();         //输出函数
   ~Book();            //析构函数
};
Book::Book(char *a,char *b,int c)
{
  title=new char[strlen(a)+1];
  strcpy(title,a);//
  author= new char[strlen(b)+1]//
  strcpy(author,b);
  sales=c;
}
void Book::print()
{
  cout<
  delete []title; //
  delete []author;//
}
int main( )
{
  char title[80],author[80];
  int sales;
  cin.getline(title,79,'n');
  cin.getline(author,79,'n');
  cin>>sales;
  Book aBook(title,author,sales);
  aBook.print();    
}

5-2
阅读下面的程序,完成其中复制构造函数的代码。

#include 
using namespace std;
class CAT
{     public:
           CAT();
           CAT(const CAT&);
          ~CAT();
          int GetAge() const { return *itsAge; }
          void SetAge(int age){ *itsAge=age; }
      protected:
          int* itsAge;
};
CAT::CAT()
{    itsAge=new int;
     *itsAge =5;
}
CAT::CAT(const CAT& c)
{
     itsAge=new int; //
    *itsAge=*(c.itsAge); //
}
CAT::~CAT()
{     delete itsAge;   }
函数题 6-1 定义一个矩形类(C++构造函数)
6-1 定义一个矩形类(C++构造函数)
设计一个名为Rectangle的矩形类,这个类包括:两个名为width和height的double数据域,它们分别表示矩形的宽和高。width和height的默认值都为1.该类包括矩形类的无参构造函数(默认构造函数);一个width和height为指定值的矩形构造函数;一个名为getArea( )的函数返回矩形的面积;一个名为getPerimeter( )的函数返回矩形的周长。请实现这个类。

类名为:
Rectangle
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
#include 
using namespace std;
//你提交的代码将嵌入到这里
 
int main()
{    
    double m,n;
    cin>>m;
    cin>>n;
    Rectangle a(m,n);
    cout< 

仅供参考

class Rectangle
{
	private:
		double width;//宽
		double height;//高
	public:
		Rectangle()//无参构造函数
		{
			width=1;
			height=1;
		}
		Rectangle(double x,double y)//有参构造函数
		{
			width=x;
			height=y;
		}
		double getArea()//得到面积
		{
			double area;
			area=width*height;
			//cout<<"面积为:"<
			return area;
		}
		double getPerimeter()//得到周长
		{
			double perimeter;
			perimeter=2*(width+height);
			//cout<<"周长为:"< 
6-2 设计一个三角形Triangle类(C++构造函数) 
6-2 设计一个三角形Triangle类(C++构造函数)
分数 10
作者 张德慧
单位 西安邮电大学
设计一个三角形Triangle类。这个类包括: 两个名为width和height的double型数据域,它们分别表示三角形的底宽和高。一个为width和height指定初值的构造函数。 一个名为getArea()的方法返回这个三角形的面积。

类名为:
Triangle
裁判测试程序样例:
#include  
using namespace std;  
// 你提交的代码将被嵌入到这里

int main( )
{
  double w,h;
  cin>>w>>h;
  Triangle t(w,h);
  cout<<"The area of the Triangle is: "< 

仅供参考

class Triangle
{
	private:
		double width;//宽
		double height;//高
	public:
		Triangle(double x,double y)//有参构造函数
		{
			width=x;
			height=y;
		}
		double getArea()//得到面积
		{
			double area;
			area=width*height/2;
			return area;
		}

};
编程题 7-1 模拟EXCEL排序
7-1 模拟EXCEL排序
分数 25
作者 陈越
单位 浙江大学
Excel可以对一组纪录按任意指定列排序。现请编写程序实现类似功能。

输入格式:
输入的第一行包含两个正整数N(≤10 
5
 ) 和C,其中N是纪录的条数,C是指定排序的列号。之后有 N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,保证没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩([0, 100]内的整数)组成,相邻属性用1个空格隔开。

输出格式:
在N行中输出按要求排序后的结果,即:当C=1时,按学号递增排序;当C=2时,按姓名的非递减字典序排序;当C=3时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

输入样例:
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
输出样例:
000001 Zoe 60
000007 James 85
000010 Amy 90

仅供参考

#include
#include
#include
#include

using namespace std;

struct student {
	string num;
	string name;
	int score = 0;
};

vectorv;

bool num(const student a, const student b) { if (a.num > b.num) return false; return true; }
bool name(const student a, const student b) { if (a.name > b.name || (a.name == b.name && a.num > b.num)) return false; return true;}
bool score(const student a, const student b) { if (a.score > b.score || (a.score == b.score && a.num > b.num)) return false; return true;}

int main(void)
{
	int N, flag;
	scanf("%d %d", &N, &flag);

	bool (*pfun) (const student, const student);
	if (flag == 1)pfun = num;
	else if (flag == 2)pfun = name;
	else pfun = score;

	student s;
	char num[7], name[9];
	for (int i = 0; i < N; i++) {
		scanf("%s %s %d", num, name, &s.score);
		s.num = num;
		s.name = name;
		v.push_back(s);
	}

	sort(v.begin(), v.end(), *pfun);

	for (int i = 0; i < N; i++) {
		printf("%s %s %dn", v[i].num.c_str(), v[i].name.c_str(), v[i].score);
	}

	return 0;
}
4+ 判断题

C++程序中,类的构造函数名与类名相同。 T

单选题

2-1
在下面类声明中,关于生成对象不正确的是( )。 C

class point
{ public:
int x;
int y;
point(int a,int b) {x=a;y=b;}
};

A.point p(10,2);
B.point *p=new point(1,2);
C.point *p=new point[2];
D.point *p[2]={new point(1,2), new point(3,4)};

2-2
所有类都应该有:C
A.构造函数
B.析构函数
C.构造函数和析构函数
D.以上答案都不对

2-3
析构函数可以返回:D
A.指向某个类的指针
B.某个类的对象
C.状态信息表明对象是否被正确地析构
D.不可返回任何值

2-4
下列属于类的析构函数特征的是 A
A.一个类中只能定义一个析构函数
B.析构函数名与类名不同
C.析构函数的定义只能在类体内
D.析构函数可以有一个或多个参数

2-5
建立一个类对象时,系统自动调用 A
A.构造函数
B.析构函数
C.友元函数
D.成员函数

2-6
以下说法中正确的是 C
A.一个类一定会有无参构造函数
B.构造函数的返回值类型是void
C.一个类只能定义一个析构函数,但可以定义多个构造函数
D.一个类只能定义一个构造函数,但可以定义多个析构函数

2-7
下列函数中,( )不能重载。 C
A.成员函数
B.非成员函数
C.析构函数
D.构造函数

2-8
下列对重载函数的描述中,( )是错误的。 A
A.重载函数中不允许使用默认参数
B.重载函数中编译根据参数表进行选择
C.不要使用重载函数来描述毫无相干的函数
D.构造函数重载将会给初始化带来多种方式

2-9
下列关于构造函数的描述中,错误的是()B
A.构造函数名与类名相同
B.构造函数可以有返回值
C.构造函数可以重载
D.每个类都有构造函数

函数题 6-1 定义一个矩形类(C++构造函数)
6-1 定义一个矩形类(C++构造函数)
设计一个名为Rectangle的矩形类,这个类包括:两个名为width和height的double数据域,它们分别表示矩形的宽和高。width和height的默认值都为1.该类包括矩形类的无参构造函数(默认构造函数);一个width和height为指定值的矩形构造函数;一个名为getArea( )的函数返回矩形的面积;一个名为getPerimeter( )的函数返回矩形的周长。请实现这个类。

类名为:
Rectangle
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
#include 
using namespace std;
//你提交的代码将嵌入到这里
 
int main()
{    
    double m,n;
    cin>>m;
    cin>>n;
    Rectangle a(m,n);
    cout< 

仅供参考

class Rectangle
{
	private:
		double width;//宽
		double height;//高
	public:
		Rectangle()//无参构造函数
		{
			width=1;
			height=1;
		}
		Rectangle(double x,double y)//有参构造函数
		{
			width=x;
			height=y;
		}
		double getArea()//得到面积
		{
			double area;
			area=width*height;
			//cout<<"面积为:"<
			return area;
		}
		double getPerimeter()//得到周长
		{
			double perimeter;
			perimeter=2*(width+height);
			//cout<<"周长为:"< 
6-2 Point类的声明和实现 
6-2 Point类的声明和实现
定义一个Point类,代表平面上的一个点,其横坐标和纵坐标分别用x和y表示,设计Point类的成员函数,实现并测试这个类。
主函数中输入两个点的坐标,计算并输出了两点间的距离。请根据主函数实现Point类。

裁判测试程序样例:
#include 
#include 
#include 
using namespace std;

//你的代码将被嵌在这里

int main()
{
    Point p1, p2;
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    p1.setX(x1);
    p1.setY(y1);
    p2.setX(x2);
    p2.setY(y2);
    double x = p1.getX() - p2.getX();
    double y = p1.getY() - p2.getY();
    double len = sqrt(x * x + y * y);
    cout << fixed << setprecision(2) << len << endl;
    return 0;
}
输入样例:
0 0 3 3
输出样例:
4.24

仅供参考

class Point 
{
private:
    int x, y;
public:
    void setX(double x1) { x = x1; }
    void setY(double y1) { y = y1; }
    double getX() { return x; }
    double getY() { return y; }
};

6-3 学生排名表(析构函数)
6-3 学生排名表(析构函数)
现在输入一批学生(人数大于0且不超过100)的名次和他们的姓名。要求按名次输出每个人的排名。

输入格式:每行为一个学生的信息,共两项,第一项为排名(为正整数,且任意两名学生的排名均不同),第二项为学生姓名。当输入-1时,表示输入结束。

输出格式:按名次输出学生姓名,每行一个。

函数接口定义:
main函数的一部分。
裁判测试程序样例:
#include 
#include 
using namespace std;
class Student{
    int rank;
    string name;
    public:
        int getRank(){return rank;    }
        Student(string name, int rank):name(name), rank(rank){    }
        ~Student(){ cout<
    int rank, count=0;
    const int SIZE=100;
    Student *pS[SIZE];
    string name;
    cin>>rank;
    while(count0){
        cin>>name;
        pS[count++]= new Student(name, rank);
        cin>>rank;
    }



    return 0;
}
输入样例:
1 Jack
5 Peter
2 Alice
6 Kate
52 Mike
-1
输出样例:
Jack
Alice
Peter
Kate
Mike

仅供参考

   for(int i=1;i
        for(int j=0;j
          if(pS[j]->getRank()>pS[j+1]->getRank()){
            Student *temp=pS[j];
            pS[j]=pS[j+1];
            pS[j+1]=temp;

                      }
        }
    }

   for(int i=0;i
    delete pS[i];
   }

6-4 体育俱乐部I(构造函数)
6-4 体育俱乐部I(构造函数)
一个俱乐部需要保存它的简要信息,包括四项:名称(字符串),成立年份(整数),教练姓名(字符串)和教练胜率(0-100之间的整数)。用键盘输入这些信息后,把它们分两行输出:第一行输出名称和成立年份,第二行输出教练姓名和胜率。

裁判测试程序样例:
#include 
#include 
using namespace std;
class Coach{
    string name;
    int winRate;
public:
    Coach(string n, int wr){
        name=n; winRate=wr;
    }
    void show();
};
class Club{
    string name;
    Coach c;
    int year;
public:
    Club(string n1, int y, string n2, int wr);
    void show();
};
int main(){
    string n1, n2;
    int year, winRate;
    cin>>n1>>year>>n2>>winRate;
    Club c(n1,year, n2, winRate);
    c.show();
    return 0;
}


输入样例:
Guanzhou 2006 Tom 92
输出样例:
Guanzhou 2006
Tom 92%

仅供参考

void Coach::show()
{
  cout<
  year=y;
  name=n1;
}
void Club::show()
{
  cout<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/850836.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号