accessor and mutator c++在对象中如何使用
时间: 2024-05-01 15:18:07 浏览: 122
Accessor和Mutator在面向对象编程中通常是用于访问和修改对象属性的方法。在C语言中,我们可以使用结构体来模拟对象,然后在结构体中定义成员变量和成员函数来实现Accessor和Mutator的功能。具体地说,我们可以在结构体中声明public和private的成员变量,然后通过public的成员函数来访问和修改private成员变量,这样就保证了数据的封装性和安全性。例如:
```
//定义一个结构体来模拟对象
typedef struct {
int age; //private成员变量
} Person;
//定义public的成员函数来访问和修改private成员变量
int get_age(Person *p) {
return p->age;
}
void set_age(Person *p, int age) {
p->age = age;
}
```
这样,我们就可以在C语言中实现类似于Accessor和Mutator的功能了。
相关问题
Define a Pet class that stores the pet’s name, age, and weight. Add appropriate constructors, accessor, and mutator functions. Also define a function named getLifespan that returns a string with the value “unknown lifespan.” Next, define a Dog class that is derived from Pet. The Dog class should have a private member variable named breed that stores the breed of the dog. Add mutator and accessor functions for the breed variable and appropriate constructors. Redefine the getLifespan function to return "Approximately 7 years" if the dog’s weight is over 100 pounds and "Approximately 13 years" if the dog’s weight is under 100 pounds. Next, define a Rock class that is derived from Pet. Redefine the getLifespan function to return “Thousands of years”. Finally, write a test program that creates instances of pet rocks and pet dogs that exercise the inherited and redefined functions in C++.
以下为C++代码实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 宠物类
class Pet {
protected:
string name;
int age;
double weight;
public:
Pet(string name, int age, double weight) {
this->name = name;
this->age = age;
this->weight = weight;
}
string getName() {
return name;
}
int getAge() {
return age;
}
double getWeight() {
return weight;
}
void setName(string name) {
this->name = name;
}
void setAge(int age) {
this->age = age;
}
void setWeight(double weight) {
this->weight = weight;
}
virtual string getLifespan() {
return "unknown lifespan";
}
};
// 狗类
class Dog : public Pet {
private:
string breed;
public:
Dog(string name, int age, double weight, string breed)
: Pet(name, age, weight) {
this->breed = breed;
}
string getBreed() {
return breed;
}
void setBreed(string breed) {
this->breed = breed;
}
virtual string getLifespan() {
if (weight > 100) {
return "Approximately 7 years";
} else {
return "Approximately 13 years";
}
}
};
// 岩石类
class Rock : public Pet {
public:
Rock(string name, int age, double weight)
: Pet(name, age, weight) {}
virtual string getLifespan() {
return "Thousands of years";
}
};
// 测试代码
int main() {
Pet* pet1 = new Rock("Rocky", 1000, 1000);
Pet* pet2 = new Dog("Fido", 3, 80, "Golden Retriever");
cout << pet1->getName() << ", " << pet1->getLifespan() << endl;
cout << pet2->getName() << ", " << pet2->getLifespan() << endl;
delete pet1;
delete pet2;
return 0;
}
```
运行结果为:
```
Rocky, Thousands of years
Fido, Approximately 13 years
```
c++ Define a Pet class that stores the pet’s name, age, and weight. Add appropriate constructors, accessor, and mutator functions. Also define a function named getLifespan that returns a string with the value “unknown lifespan.” Next, define a Dog class that is derived from Pet. The Dog class should have a private member variable named breed that stores the breed of the dog. Add mutator and accessor functions for the breed variable and appropriate constructors. Redefine the getLifespan function to return "Approximately 7 years" if the dog’s weight is over 100 pounds and "Approximately 13 years" if the dog’s weight is under 100 pounds. Next, define a Rock class that is derived from Pet. Redefine the getLifespan function to return “Thousands of years”. Finally, write a test program that creates instances of pet rocks and pet dogs that exercise the inherited and redefined functions.
下面是 C++ 的实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Pet {
private:
string name;
int age;
double weight;
public:
Pet(string n, int a, double w) {
name = n;
age = a;
weight = w;
}
string getName() { return name; }
int getAge() { return age; }
double getWeight() { return weight; }
void setName(string n) { name = n; }
void setAge(int a) { age = a; }
void setWeight(double w) { weight = w; }
virtual string getLifespan() {
return "unknown lifespan";
}
};
class Dog : public Pet {
private:
string breed;
public:
Dog(string n, int a, double w, string b) : Pet(n, a, w) {
breed = b;
}
string getBreed() { return breed; }
void setBreed(string b) { breed = b; }
virtual string getLifespan() {
if (getWeight() > 100) {
return "Approximately 7 years";
}
else {
return "Approximately 13 years";
}
}
};
class Rock : public Pet {
public:
Rock(string n, int a, double w) : Pet(n, a, w) {}
virtual string getLifespan() {
return "Thousands of years";
}
};
int main() {
// create instances of pet rocks and pet dogs
Pet* pet1 = new Rock("Rocky", 100, 10);
Pet* pet2 = new Dog("Fido", 5, 150, "Golden Retriever");
// exercise the inherited and redefined functions
cout << "Pet 1 - Name: " << pet1->getName() << ", Age: " << pet1->getAge()
<< ", Weight: " << pet1->getWeight() << ", Lifespan: " << pet1->getLifespan() << endl;
cout << "Pet 2 - Name: " << pet2->getName() << ", Age: " << pet2->getAge()
<< ", Weight: " << pet2->getWeight() << ", Breed: " << ((Dog*)pet2)->getBreed()
<< ", Lifespan: " << pet2->getLifespan() << endl;
// free memory
delete pet1;
delete pet2;
return 0;
}
```
这个程序中,我们定义了一个 Pet 类,它有三个私有成员变量:名字、年龄和体重,以及相应的访问器和修改器。Pet 类还定义了一个虚函数 getLifespan,它返回字符串 "unknown lifespan"。
然后我们定义了一个 Dog 类,它继承自 Pet 类,并添加了一个私有成员变量 breed,以及相应的访问器和修改器。Dog 类还重新定义了 getLifespan 函数,根据狗的体重返回不同的字符串。
最后我们定义了一个 Rock 类,它也继承自 Pet 类,并重新定义了 getLifespan 函数,返回字符串 "Thousands of years"。
在主函数中,我们创建了两个 Pet 对象,一个是 Rock,一个是 Dog,然后分别调用它们的函数,验证了继承和重载的正确性。
阅读全文