0

C++ provides two similar provisions to perform the same task.While object composition seems more convenient as the declared class can be used for some other class as well.

Eg. An 'Address' class can contain some properties and functions and then be used as a property of a 'Student' class. Alternatively,the 'Address' class can be declared inside the 'Student' class(nested).

So my doubt is,which of these methods should be used and when? Also is one considered a better approach than the other?

2 Answers2

0

Splitting a Student class into an Address class doesn't make much sense unless there are other "things" that need an Address too (see below) - a more reasonable approach would be to have a Person class, and then derive Student from Person - as well as Teacher, Dean or whatever other classes may be needed to describe a school. The key here is to look at "what is common, what is separate".

But there may be cases where an Address or Addressable class is useful. If we want to keep track of families, and want to have only one item of Address for the whole family of two adults, two children, one dog, one house and one car in some form of database (say an insurance company), then having a separate Address may indeed make sense. If a son (or daughter) in the household then moves to go to university, he will get a new separate address (along with a bill to cover the extra risk of being at university vs. living with mum & dad).

Or if we have to track things that are not Person - say you don't have just people, but also buildings and equipment that need to have an Address. Then having a common set of functionality for "non-person" and "person" would make more sense.

It really depends on what you aim to achieve, and you really need to think through the usage of the data. There is no actual fixed and firm answer. Splitting too much is just as bad as combining too much.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

If i correctly understand your question, it is about whether the class defining a subobject (here Address) should be declared at same level as the containing class (here Student) in inside the containing class.

The main difference will be the access to private or protected members of the enclosing class from the subobject. A nested class being a member of its enclosing class will have access to all private members (ref: 11.7 Nested classes [class.access.nest] in n4296 draft for C++ 11).

Here is an example where a nested class access to private members of its enclosing class:

class Person {
public:
    class Address {
    public:
        std::string street;
        std::string town;
        std::string state;

        Address(std::string street, std::string town, std::string state)
            :street(street), town(town), state(state) {}
        void print(std::ostream&, const Person& p) const;
    };

    Person(std::string firstname, std::string lastname, Address address);
private:
    std::string firstname; // private member
    std::string lastname;
    Address address;
    // other members ...
};

Person::Person(std::string firstname, std::string lastname, Address address)
    :firstname(firstname), lastname(lastname), address(address) {};

void Person::Address::print(std::ostream& out, const Person& p) const {
    // access to private members of Person class
    out << p.firstname << " " << p.lastname << std::endl;
    out << street << std::endl;
    out << town << std::endl;
    out << state << std::endl;
}
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252