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

3 C++【C++】

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

3 C++【C++】

3- 单选题

2-1 给定以下类声明,哪个成员函数可能改变成员变量data? D

class A {

public:

 void f1 (int d);
 void f2 (const int &d);
 void f3 (int d) const;
private:

   int data;
};

A.f1
B.f2
C.f3
D.f1和f2

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

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

2-4类成员的默认访问属性是: A
A.private
B.protected
C.public
D.以上答案都不对

2-5在面向对象的软件系统中,不同类对象之间的通信的一种构造称为_______。 D
A.属性
B.封装
C.类
D.消息

填空题

4-1
A class is a user-defined type.
A class consists of a set of members. The most common kinds of members are data members and member functions.
A class is a namespace containing its members.

4-2
C++ classes are a tool for creating new types that can be used as conveniently as the built-in types.
The fundamental idea in defining a new type is to separate the incidental details of the implementation(e.g., the layout of the data used to store an object of the type) from the properties essential to the correct use of it (e.g., the complete list of functions that can access the data). Such a separation is best expressed by channeling all uses of the data structure and its internal housekeeping routines through a specific interface.

函数题 6-1 设计一个名为Rectangle的矩形类(C++ set函数)
6-1 设计一个名为Rectangle的矩形类(C++ set函数)
分数 10
作者 张德慧
单位 西安邮电大学
设计一个名为Rectangle的矩形类,这个类包括:两个名为width和height的double数据域,它们分别表示矩形的宽和高。一个为width和height设置初值的函数set( );一个名为getArea( )的函数返回矩形的面积;一个名为getPerimeter( )的函数返回矩形的周长。请实现这个类。

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

仅供参考

class Rectangle  {
private:
    double width;
    double height;
public:
    int set(double m, double n) {
        width = m;
        height = n;
        return 0;
    }
    double getArea() {
        return width * height;
    }
    double getPerimeter() {
        return 2 * (width + height);
    }
};

6-2 Point类的声明和实现
6-2 Point类的声明和实现
分数 10
作者 李廷元
单位 中国民用航空飞行学院
定义一个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{
    double x;
    double y;
  public:
     void setX(double a)
     {
       x=a;
     }
     void setY(double b)
     {
       y=b;
     }
     double getX()
     {
       return x;
     }
     double getY()
     {
       return y;
     }
};
3+ 单选题

2-1
有关类和对象的说法下列不正确的有( )。 C

A.对象是类的一个实例
B.任何一个对象只能属于一个具体的类
C.一个类只能有一个对象
D.类与对象和关系与数据类型和变量的关系相似

2-2
类的实例化是指( )。 B
A.定义类
B.定义对象
C.调用类的成员函数
D.访问对象的数据成员

2-3
关于成员函数特征的描述中,( )是错误的。 B
A.成员函数可以重载
B.成员函数一定是内联函数
C.一个类可以没有成员函数
D.成员函数可以设置参数的默认值

2-4
如果类定义中没有使用 private、protected、或public 关键字,则所有成员( )C
A.都是 public 成员
B.都是 proctected 成员
C.都是 private 成员
D.不一定

2-5
在面向对象系统中,对象是基本的运行时实体,它 _____ 。 C
A.只能包括数据(属性)
B.只能包括操作(行为)
C.把属性和行为封装为一个整体
D.必须具有显式定义的对象名

2-6
在面向对象系统中,对象的属性是________。 C
A.对象的行为特性
B.和其他对象相关联的方式
C.和其他对象相互区分的特性
D.与其他对象交互的方式

填空题

4-1
The public members provide the class’s 在这里插入代码片and the private members provide implementation details. Members are accessed using . (dot) for objects and −> (arrow) for pointers.

4-2
A struct is a class where members are by default 在这里插入代码片.

函数题 6-1 类的声明和成员函数的实现
6-1 类的声明和成员函数的实现
分数 10
作者 李廷元
单位 中国民用航空飞行学院
声明了一个Dog类,包含了age,weight等属性,以及对这些属性进行操作的方法。请实现该类的成员函数。

Dog类声明如下:
class Dog {
public:
    void setAge(int a);
    int getAge();
    void setWeight(int w);
    int getWeight();
private:
    int age, weight;
};
请实现Dog类的成员函数。

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

class Dog {
public:
    void setAge(int a);
    int getAge();
    void setWeight(int w);
    int getWeight();
private:
    int age, weight;
};

int main()
{
    Dog d;
    int a, w;
    cin >> a >> w;
    d.setAge(a);
    d.setWeight(w);
    cout << d.getAge() << endl;
    cout << d.getWeight() << endl;
    return 0;
}


输入样例:
1 3
输出样例:
1
3

仅供参考

void Dog :: setAge(int a) {
    age = a;
}
void Dog :: setWeight(int w) {
    weight = w;
}
int Dog :: getAge() {
    return age;
}
int Dog :: getWeight() {
    return weight;
}

6-2 使用类计算矩形的面积
6-2 使用类计算矩形的面积
分数 10
作者 李廷元
单位 中国民用航空飞行学院
定义并实现一个矩形类,有长和宽两个属性,由成员函数计算矩形的面积。

矩形类Rectang接口定义如下:
class Rectangle {
public:
    void setLength(int l);//设置矩形的长度
    void setWidth(int w); //设置矩形的宽度
    int getArea();    //计算并返回矩形的面积
private:
    int length, width;  //矩形的长度和宽度    
};
请实现Rectangle类的成员函数。

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

class Rectangle {
public:
    void setLength(int l);//设置矩形的长度
    void setWidth(int w); //设置矩形的宽度
    int getArea();        //计算并返回矩形的面积
private:
    int length, width;    //矩形的长度和宽度    
};

int main()
{
    Rectangle r;
    int len, w;
    cin >> len >> w;
    r.setLength(len);
    r.setWidth(w);
    cout << r.getArea() << "n";

    return 0;
}


输入样例:
10 20
输出样例:
200

仅供参考

void Rectangle :: setLength(int l) {
    length = l;
}
void Rectangle :: setWidth(int w) {
    width = w;
}
int Rectangle :: getArea() {
    return length * width;
}

6-3 定义一个名为Stock的股票类
分数 10
作者 张德慧
单位 西安邮电大学
定义一个名为Stock的股票类,这个类包括:一个名为symbol的字符串表示股票代码。一个名为name的字符串数据成员表示股票名称。一个名为previousClosingPrice的double数据成员,它存储前一日的股票收盘价。一个名为currentPrice数据成员,它存储当前的股票成交价格。创建一个设置股票代码和股票名称的set( )函数。一个名为changePercent()函数返回股价涨跌幅度。(即从previousClosingPrice变化到currentPrice的百分比。)
###类名为:

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


int main( ) {
    char s1[10],n1[20];
    cin>>s1>>n1;
    Stock stock;
    stock.set(s1, n1);
    // 输入前一日收盘价
    cin>>stock.previousClosingPrice;
 
    // 输入当前成交价
    cin>>stock.currentPrice;

    // 显示股票信息
    cout< 

仅供参考

#include
#include
#include 
class Stock {
public: 	
	char symol[10];
	char name[20];	
	double previousClosingPrice;
	double currentPrice;
	void set(char s1[],char n1[]){
		strcpy(this->symol,s1);
		strcpy(this->name,n1);
	} 
	
	double changePercent(){
		double i;
		i=(previousClosingPrice-currentPrice)/previousClosingPrice*-1;
        return i;
	}
	 
};

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847492.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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