(1)设计一个factory (工厂) 类,完成输入、修改以及输出工厂基本信息的功能,以此理解类的定义,对象的定义和初始化,要求: A. 数据成员包括:工厂名称(name)、工厂注册码(registration_code)、注册资金(registered_capital)、产品数量(product_quantity)等,其中name为string类型,registration_code和product_quantity位整型,registered_capital为浮点型。 B. 公有成员函数包括:input_information(), update_information()和display_information()以及两个构造函数:默认构造函数、含四个参数的构造函数。input_information()用于输入信息,display_information() 用于输出信息,update_information() 用于修改信息。 C. 在main中分别用“默认构造函数+ input_information()”创建并初始化factory对象f1,用带参的构造函数定义factory对象f2,之后对每个对象调用display_information()显示对象信息,调用update_ information () 更新信息后再显示对象信息。 D. 将B中的若干个构造函数用使用默认实参值的构造函数代替,完成C。 (2)A. 思考:在类的构造函数中可以对类的静态数据成员初始化吗? B. 给factory增加一个静态成员变量next_code,在默认构造函数中用next_code对registration_code初始化,其余数据成员初始化为0或空。将factory类的定义放在factory.h中,将factory类的实现放在factory.cpp中,在main中用默认构造函数定义三个factory对象,调用display_information() 显示对象信息,以此理解并掌握静态数据成员,类的接口和实现的分离。
时间: 2023-06-14 22:04:42 浏览: 112
类的static,const,static const,const static成员的初始化
3星 · 编辑精心推荐
(1)factory类的定义和实现如下:
factory.h
```cpp
#ifndef FACTORY_H
#define FACTORY_H
#include <string>
class factory {
public:
factory();
factory(const std::string& name, int registration_code, float registered_capital, int product_quantity);
void input_information();
void update_information();
void display_information() const;
private:
std::string name;
int registration_code;
float registered_capital;
int product_quantity;
};
#endif // FACTORY_H
```
factory.cpp
```cpp
#include "factory.h"
#include <iostream>
factory::factory() : registration_code(0), registered_capital(0), product_quantity(0) {}
factory::factory(const std::string& name, int registration_code, float registered_capital, int product_quantity)
: name(name), registration_code(registration_code), registered_capital(registered_capital), product_quantity(product_quantity) {}
void factory::input_information() {
std::cout << "Enter factory name: ";
std::cin >> name;
std::cout << "Enter registration code: ";
std::cin >> registration_code;
std::cout << "Enter registered capital: ";
std::cin >> registered_capital;
std::cout << "Enter product quantity: ";
std::cin >> product_quantity;
}
void factory::update_information() {
std::cout << "Enter new registered capital: ";
std::cin >> registered_capital;
std::cout << "Enter new product quantity: ";
std::cin >> product_quantity;
}
void factory::display_information() const{
std::cout << "Factory name: " << name << std::endl;
std::cout << "Registration code: " << registration_code << std::endl;
std::cout << "Registered capital: " << registered_capital << std::endl;
std::cout << "Product quantity: " << product_quantity << std::endl;
}
```
在main中,我们可以按照以下方式创建并初始化factory对象:
```cpp
#include "factory.h"
int main() {
factory f1;
f1.input_information();
f1.display_information();
factory f2("Factory 2", 123456, 1000000.0, 500);
f2.display_information();
f1.update_information();
f1.display_information();
f2.update_information();
f2.display_information();
return 0;
}
```
(2)A. 类的静态数据成员可以在类的定义中进行初始化,但是不能在构造函数中初始化。因为静态数据成员属于类,而不是类的对象,在构造函数中无法访问它。
B. 增加静态成员变量next_code和默认构造函数的实现如下:
factory.h
```cpp
#ifndef FACTORY_H
#define FACTORY_H
#include <string>
class factory {
public:
factory();
void input_information();
void update_information();
void display_information() const;
private:
std::string name;
int registration_code;
float registered_capital;
int product_quantity;
static int next_code;
};
#endif // FACTORY_H
```
factory.cpp
```cpp
#include "factory.h"
#include <iostream>
int factory::next_code = 100000;
factory::factory() : registration_code(next_code++), registered_capital(0), product_quantity(0) {}
void factory::input_information() {
std::cout << "Enter factory name: ";
std::cin >> name;
std::cout << "Enter registered capital: ";
std::cin >> registered_capital;
std::cout << "Enter product quantity: ";
std::cin >> product_quantity;
}
void factory::update_information() {
std::cout << "Enter new registered capital: ";
std::cin >> registered_capital;
std::cout << "Enter new product quantity: ";
std::cin >> product_quantity;
}
void factory::display_information() const{
std::cout << "Factory name: " << name << std::endl;
std::cout << "Registration code: " << registration_code << std::endl;
std::cout << "Registered capital: " << registered_capital << std::endl;
std::cout << "Product quantity: " << product_quantity << std::endl;
}
```
在main中,我们可以按照以下方式创建并初始化factory对象:
```cpp
#include "factory.h"
int main() {
factory f1;
f1.input_information();
f1.display_information();
factory f2;
f2.input_information();
f2.display_information();
factory f3;
f3.input_information();
f3.display_information();
return 0;
}
```
阅读全文