1、领养宠物并打印宠物信息:
(1)创建宠物狗狗类(Dog)
狗狗类的属性: 昵称(name)、品种(strain);
狗狗类的方法(print):打印自己的信息,含上述属性值
(2)创建宠物企鹅类(Penguin)
企鹅类的属性:昵称、性别(sex);
企鹅类的方法:打印自己的信息
(3)创建测试app“卖宠物”,首先app给顾客打招呼并介绍宠物类型(狗狗和企鹅),让顾客选择宠物(狗狗或企鹅),打印出顾客选择的宠物的基本信息。
c++请定义类
class Dog
{
private:
string name;
string strain;
public:
Dog(string name, string strain)
{
this->name = name;
this->strain = strain;
}
void print()
{
cout << "Pet dog: Name=" << this->name << ", Strain=" << this->strain << endl;
}
};
class Penguin
{
private:
string name;
string sex;
public:
Penguin(string name, string sex)
{
this->name = name;
this->sex = sex;
}
void print()
{
cout << "Pet penguin: Name=" << this->name << ", Sex=" << this->sex << endl;
}
};
int main()
{
// App starts with greeting
cout << "Welcome to the Pet Store!" << endl;
// Introduce available pets
cout << "We have two kinds of pets available: dogs and penguins." << endl;
// Ask user to choose pet type
cout << "Please select a pet type (Enter 1 for dog, 2 for penguin): ";
int petType;
cin >> petType;
// Based on user input, create and print pet information
if (petType == 1)
{
cout << "You have selected a dog. Please enter nickname: ";
string name;
cin >> name;
cout << "Please enter strain: ";
string strain;
cin >> strain;
Dog dog(name, strain);
dog.print();
}
else if (petType == 2)
{
cout << "You have selected a penguin. Please enter nickname: ";
string name;
cin >> name;
cout << "Please enter sex (M for male, F for female): ";
string sex;
cin >> sex;
Penguin penguin(name, sex);
penguin.print();
}
else
{
cout << "Invalid choice. Please choose 1 or 2." << endl;
}
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话: