当前位置:网站首页>Third class exercise
Third class exercise
2022-07-28 14:46:00 【Qing Ankang】
Catalog
One 、 Single topic selection
2-1
The following incorrect statements about classes and objects are ( C).
A. Object is an instance of a class
B. Any object can only belong to a specific class
C. A class can only have one object
D. The relationship between classes and objects is similar to that between data types and variables
2-2
Class instantiation refers to ( B).
A. Defining classes
B. Define the object
C. Call the member function of the class
D. Access the data members of the object
2-3
In the description of the characteristics of member functions ,(B ) It's wrong. .
A. Member functions can be overloaded
B. Member functions must be inline functions
C. A class can have no member functions
D. Member functions can set the default value of parameters
2-4
If... Is not used in the class definition private、protected、 or public keyword , Then all members ( C)
A. All are public member
B. All are proctected member
C. All are private member
D. not always
2-5
In an object-oriented system , Objects are basic runtime entities , it C___ .
A. Only data can be included ( attribute )
B. Only operations can be included ( Behavior )
C. Encapsulate attributes and behaviors as a whole
D. Must have an explicitly defined object name
2-6
In an object-oriented system , The property of an object is __C______.
A. Behavior characteristics of objects
B. How to associate with other objects
C. Distinguishing features from other objects
D. How to interact with other objects
Two 、 Completion
1.(4 branch )The public members provide the class’s interface and the private members provide implementation details. Members are accessed using . (dot) for objects and −> (arrow) for pointers.
2.(2 branch )A struct is a class where members are by default public
3、 ... and 、 Function questions
6-1 Class declaration and implementation of member functions
fraction 10
author Li Tingyuan
Company China Academy of Civil Aviation
Declared a Dog class , Contains age,weight Equal attribute , And how to operate on these properties . Please implement the member function of this class .
Dog Class is declared as follows :
class Dog {
public:
void setAge(int a);
int getAge();
void setWeight(int w);
int getWeight();
private:
int age, weight;
};
Please implement Dog class .
Sample referee test procedure :
#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;
}
/* Your code will be embedded here */
sample input :
1 3
sample output :
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 Use the class to calculate the area of the rectangle
fraction 10
author Li Tingyuan
Company China Academy of Civil Aviation
Define and implement a rectangle class , It has two properties: length and width , Calculate the area of the rectangle by the member function .
Rectangle class Rectang The interface is defined as follows :
class Rectangle {
public:
void setLength(int l);// Set the length of the rectangle
void setWidth(int w); // Set the width of the rectangle
int getArea(); // Calculates and returns the area of the rectangle
private:
int length, width; // The length and width of the rectangle
};
Please implement Rectangle class .
Sample referee test procedure :
#include
using namespace std;
class Rectangle {
public:
void setLength(int l);// Set the length of the rectangle
void setWidth(int w); // Set the width of the rectangle
int getArea(); // Calculates and returns the area of the rectangle
private:
int length, width; // The length and width of the rectangle
};
int main()
{
Rectangle r;
int len, w;
cin >> len >> w;
r.setLength(len);
r.setWidth(w);
cout << r.getArea() << “\n”;
return 0;
}
/* Your code will be embedded here */
sample input :
10 20
sample output :
200
void Rectangle :: setLength(int l) {
length = l;
}
void Rectangle :: setWidth(int w) {
width = w;
}
int Rectangle :: getArea() {
return length * width;
}
6-3 Define a name Stock Our stock class
fraction 10
author Zhang Dehui
Company Xi'an University of Posts and Telecommunications
Define a name Stock Our stock class , This class includes : A group called symbol The string represents the stock code . A group called name The string data member of represents the stock name . A group called previousClosingPrice Of double Data member , It stores the closing price of the previous day . A group called currentPrice Data member , It stores the current stock transaction price . Create a to set the stock code and stock name set( ) function . A group called changePercent() Function returns the range of stock price rise and fall .( From previousClosingPrice Change to currentPrice Percent of .)
### Class called :
Stock
Sample referee test procedure :
#include
using namespace std;
/* The code you submit will be embedded here */
int main( ) {
char s1[10],n1[20];
cin>>s1>>n1;
Stock stock;
stock.set(s1, n1);
// Enter the closing price of the previous day
cin>>stock.previousClosingPrice;
// Enter the current transaction price
cin>>stock.currentPrice;
// Show stock information
cout<<stock.name<<" price changed: " <<(stock.changePercent() * 100)<<"%";
return 0;
}
sample input :
002594
BYD
56.98
55.40
sample output :
BYD price changed: -2.7729%
class Stock{
public:
char* symbol;
char* name;
double previousClosingPrice;
double currentPrice;
public:
void set(char* s,char* n){
symbol = s;
name = n;
}
double changePercent(){
return (currentPrice-previousClosingPrice)/previousClosingPrice;
}
};
边栏推荐
- 使用Weka与Excel进行简单的数据分析
- How many ways can multithread run in sequence?
- 2022 melting welding and thermal cutting examination questions and online simulation examination
- (function(global,factory){
- How long can we "eat" the dividends of domestic databases?
- FormData对象的使用, var formdata=new FormData()
- Floating point data type in C language (did you learn to waste it)
- linux安装redis
- Brief introduction of diversity technology
- Development and definition of software testing
猜你喜欢

企鹅一面:为什么不建议使用SELECT * ?

Focus on differentiated product design, intelligent technology efficiency improvement and literacy education around new citizen Finance

Hcip day 12

Redis redis use in jedis

How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally

How long can we "eat" the dividends of domestic databases?
![[Tanabata] Tanabata lonely little frog research edition? The final chapter of Tanabata Festival!](/img/0b/4fc583a3dd4794b0c2b0d64d905be7.png)
[Tanabata] Tanabata lonely little frog research edition? The final chapter of Tanabata Festival!

数字化转型安全问题频发,山石网科助力数字政府建设

Copy excel row to specified row

如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
随机推荐
SwiftUI 4.0 的全新导航系统
2022 high altitude installation, maintenance, removal of examination question bank and online simulated examination
Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it
为什么jq的匿名函数 外部可以访问到里面的方法
工厂模式和构造函数模式
Thrift 序列化协议浅析
力扣解法汇总1331-数组序号转换
Pointers and arrays (7)
js的实例化方式
Log management platform of infrastructure and nail & email alarm notification
BGP experiment
When Xcode writes swiftui code, it is a small trap that compiles successfully but causes the preview to crash
Why can the anonymous functions of JQ access the methods inside
Career planning of Software Test Engineer
多线程顺序运行有几种方法?
Installing redis in Linux
How long can we "eat" the dividends of domestic databases?
【LeetCode】 贴纸拼词(动态规划)
Ability to add class @published for custom attribute wrapper types
Hand in hand from 0 to a "Nuggets special attention" Google plug-in, 5000 words detailed vue3 responsive principle, the advantages, disadvantages and choices of several cache read-write schemes, flyin