Address - Person
You are given code where the address is integrated into the Person class. This isn't following the single responsibility principle. Separate the class information into two distinct classes: Person and Address. The Person class should have member variables for name and age, and the Address class should have member variables for street, city, state, and zip_code. Demonstrate creating a Person instance and an Address instance, setting their values, and printing them.
#include <iostream>
#include <string>
class Person {
private:
std::string name;
int age;
// Address variables
std::string street;
std::string city;
std::string state;
std::string zip_code;
public:
Person(const std::string& name, int age, const std::string& street, const std::string& city, const std::string& state, const std::string& zip_code)
: name(name), age(age), street(street), city(city), state(state), zip_code(zip_code) {}
void print_details() const {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
std::cout << "Address: " << street << ", " << city << ", " << state << ", " << zip_code << std::endl;
}
};
int main() {
Person person("John Doe", 30, "123 Main St", "Springfield", "IL", "62701");
person.print_details();
return 0;
}
Hint
- Create an
Addressclass with private member variables:std::string streetstd::string citystd::string statestd::string zip_code
- Create a
Personclass with private member variables:std::string nameint ageAddress address
- Implement constructors, getters, and setters for both classes as needed.
- Implement a
print_detailsmethod for both classes to print their information. - In the
mainfunction, create instances ofPersonandAddress, set their values, and print their details.
Answer
#include <iostream>
#include <string>
// Address class definition
struct Address {
std::string street;
std::string city;
std::string state;
std::string zip_code;
// Constructor
Address(const std::string& street, const std::string& city, const std::string& state, const std::string& zip_code)
: street(street), city(city), state(state), zip_code(zip_code) {}
// Method to print address details
void print_details() const {
std::cout << "Address: " << street << ", " << city << ", " << state << ", " << zip_code << std::endl;
}
};
// Person class definition
class Person {
private:
std::string name;
int age;
Address address;
public:
// Constructor
Person(const std::string& name, int age, const Address& address)
: name(name), age(age), address(address) {}
// Getters and setters
std::string get_name() const { return name; }
void set_name(const std::string& name) { this->name = name; }
int get_age() const { return age; }
void set_age(int age) { this->age = age; }
Address get_address() const { return address; }
void set_address(const Address& address) { this->address = address; }
// Method to print person details
void print_details() const {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
address.print_details();
}
};
int main() {
// Create an Address instance
Address address("123 Main St", "Springfield", "IL", "62701");
// Create a Person instance
Person person("John Doe", 30, address);
// Print details
person.print_details();
return 0;
}