Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

OOP (Object-Oriented Programming) aggregation is a relationship between two classes where one class contains an instance of another class as a member. It represents a "has-a" relationship, where one class has a composition of another class.

In aggregation, the contained class (referred to as the "part" class) can exist independently, and it can be shared by multiple instances of the containing class (referred to as the "whole" class). The lifespan of the part object is not controlled by the whole object. In other words, if the whole object is destroyed, the part object can still exist.

Aggregation is often represented by a member variable of one class being an object of another class. The whole class can access the members of the part class and use its functionality.

In summary, OOP aggregation represents a "has-a" relationship between classes, where one class contains an instance of another class as a member. It provides a way to represent complex relationships and build more flexible and reusable code structures.


Code Example

Address header file

//  Address.hpp
//  Aggregation Example
//  Created by Kevin Roark on 6/16/23.

#ifndef Address_hpp
#define Address_hpp

#include <string>
using namespace std;

class Address
{
private:
    //class attributes / properties
    string street;
    string city;
    string state;
    string zip;
    
public:
    // Getter methods
    string getStreet() const;
    string getCity() const;
    string getState() const;
    string getZip() const;

       // Setter methods
    void setStreet(string newStreet);
    void setCity(string newCity);
    void setState(string newState);
    void setZip(string newZip);
    
    //Class Methods
    void print();

    //constructor
    Address(string street, string city, string state, string zip);
    Address(); // default constructor
};
#endif /* Address_hpp */

This is the header file (Address.hpp) for a C++ class named Address. This class can be used to model an address, containing information such as street, city, state, and zip code. It's an example of object-oriented programming in C++, as it encapsulates the data (attributes) and functions (methods) related to an address into a single class.

Here's what each part of this code does:

  • The #ifndef, #define, and #endif lines are known as include guards. They prevent potential issues that could arise from including the same header file multiple times. Address_hpp is a macro that checks if the file has already been included in the project. If not, it defines Address_hpp and includes the file.

  • The #include <string> directive is used to include the C++ Standard Library string class, which this class uses for its attributes.

  • using namespace std; allows you to use functions and classes from the std (standard) namespace without prefixing them with std::.

  • The class Address is defined with four private attributes (street, city, state, zip) that hold the address information. Being private means they can't be accessed directly from outside the class.

  • It then declares the getter and setter methods, which are used to access and modify the private attributes of the class. The getter methods are marked as const, which means they won't modify the object.

  • A print method is declared that could be used to print the address information.

  • Two constructors are declared. The first is a parameterized constructor that takes four parameters for initializing an Address object. The second is a default constructor, which is a constructor with no parameters. If no constructor is provided in the class, the compiler provides a default constructor. In this case, the default constructor is user-defined.

Code File for Address

//  Address.cpp
//  Aggregation Example
//  Created by Kevin Roark on 6/16/23.

#include <string>
#include <iostream>
#include "Address.hpp"

//Getter methods
string Address::getStreet() const
{
    return street;
}
string Address::getCity() const
{
    return city;
}
string Address::getState() const
{
    return state;
}
string Address::getZip() const
{
    return zip;
}

//Setter Methods
void Address::setStreet(string newStreet) { 
    street = newStreet;
}
void Address::setCity(string newCity) {
    city = newCity;
}
void Address::setState(string newState) {
    state = newState;
}
void Address::setZip(string newZip) {
    zip = newZip;
}

//Class Methods
void Address::print()
{
    cout << this->getStreet() << "\n" << this->getCity() << ", ";
    cout << getState() << "\n" << getZip();   
}

//constructors
Address::Address(string street, string city, string state, string zip)
{
    this->street = street;
    this->city = city;
    this->state = state;
    this->zip = zip;
}
Address::Address()
{
    street = "NA";
    city = "NA";
    state = "NA";
    zip = "NA";
}

This C++ code file (Address.cpp) provides the implementations of the methods declared in the Address class in the Address.hpp header file.

Here's what each part of this code does:

  • The #include directives at the top include the <string> and <iostream> standard library headers, which are used in the methods of the Address class, and the "Address.hpp" header, where the Address class is declared.

  • The getter methods (getStreet(), getCity(), getState(), and getZip()) simply return the value of the respective private attributes of the Address object. They are marked as const because they do not modify any class members.

  • The setter methods (setStreet(), setCity(), setState(), and setZip()) are used to set the value of the respective private attributes of the Address object.

  • The print method is implemented to print the details of the Address object to the console. It uses the cout object from the <iostream> standard library to output the details. The this-> keyword is used to access the current object's getter methods, although it's not strictly necessary here.

  • Two constructors are implemented:

    • The parameterized constructor (Address::Address(string street, string city, string state, string zip)) sets the values of the private attributes based on the arguments provided when an Address object is created.

    • The default constructor (Address::Address()) sets all the private attributes to "NA". This constructor is called when an Address object is created without any arguments.

Overall, this code is an example of how to implement a class in C++, providing methods to access and modify the class's private attributes and constructors to initialize the objects of the class.

Person header File

//
//  Person.hpp
//  Created by Kevin Roark on 6-15-23.
#ifndef Person_hpp
#define Person_hpp

#include <stdio.h>

#endif /* Person_hpp */
#include <string>
#include "Address.hpp"
using namespace std;

class Person
{
private:
    //class attributes / properties
    string firstName;
    string middleName;
    string lastName;
    Address* homeAddress;
    
public:
    //getters and setters declarations
    void print() const;
    void setName(string first, string middle, string last);
    void setLastName(string last);
    void setFirstName(string first);
    void setMiddleName(string middle);
    void setAddress(Address myAddress);
    
    string getFirstName() const;
    string getMiddleName() const;
    string getLastName() const;
    Address getAddress() const;

    //constructor
    Person(string, string, string, Address* );
};

This is the header file (Person.hpp) for a C++ class named Person. The Person class models a person and their home address. It's another example of object-oriented programming in C++, as it encapsulates the data (attributes) and functions (methods) related to a person and their address into a single class.

Here's what each part of this code does:

  • The #ifndef, #define, and #endif lines are include guards. They prevent potential issues that could arise from including the same header file multiple times. Person_hpp is a macro that checks if the file has already been included in the project. If not, it defines Person_hpp and includes the file.

  • The #include <string> directive is used to include the C++ Standard Library string class, which this class uses for its attributes.

  • "Address.hpp" is included because this class uses Address objects as one of its attributes.

  • using namespace std; allows you to use functions and classes from the std (standard) namespace without prefixing them with std::.

  • The class Person is defined with four private attributes: firstName, middleName, lastName, and homeAddress. Being private means they can't be accessed directly from outside the class. The homeAddress attribute is a pointer to an Address object.

  • Getter and setter methods are declared for accessing and modifying the private attributes of the class. The getter methods are marked as const, which means they won't modify the object.

  • A print method is declared that could be used to print the Person object's information.

  • A constructor is declared that takes four parameters: three strings for the person's first, middle, and last names, and a pointer to an Address object for their home address.

Code file for Person

//  Person.cpp
//  Created by Kevin Roark on 6-15-23.
// This is a C++ source file that defines a class called "Person".
// The class has member functions to manipulate a person's first, middle, and last name. It demostrates aggregation using the Address class

// Include necessary header files
#include <iostream> // Input/output operations
#include <string>   // String operations
#include "Person.hpp" // User-defined header file for the Person class
#include "Address.hpp" //User-defined header file for the Address class

using namespace std;
// Define the constructor function that initializes the first, middle, and last name of a person using provided parameters and address
Person::Person(string first, string middle, string last, Address* homeAdd)
{
    firstName = first;
    middleName = middle;
    lastName = last;
    homeAddress = homeAdd;
    
}

//PUBLIC METHODS
// Define the member function "print" that prints the first, middle, and last name of a person as well as the address from the Address class
void Person::print() const
{
    cout << firstName << " " << middleName << " " << lastName << endl;
    homeAddress->print();
}

// SETTERS
// Define the member function "setName" that sets the first, middle, and last name of a person
void Person::setName(string first, string middle, string last)
{
    firstName = first;
    middleName = middle;
    lastName = last;
}

// Define the member function "setLastName" that sets the last name of a person
void Person::setLastName(string last)
{
    lastName = last;
}

// Define the member function "setFirstName" that sets the first name of a person
void Person::setFirstName(string first)
{
    firstName = first;
}

// Define the member function "setMiddleName" that sets the middle name of a person
void Person::setMiddleName(string middle)
{
    middleName = middle;
}

//GETTERS
// Define the member function "getFirstName" that returns the first name of a person
string Person::getFirstName() const
{
    return firstName;
}

// Define the member function "getMiddleName" that returns the middle name of a person
string Person::getMiddleName() const
{
    return middleName;
}

// Define the member function "getLastName" that returns the last name of a person
string Person::getLastName() const
{
    return lastName;
}

Address Person::getAddress() const
{
    return *homeAddress;
}

This C++ code file (Person.cpp) provides the implementations for the methods declared in the Person class in the Person.hpp header file.

Here's what each part of the code does:

  • The #include directives at the top include the standard library headers for input/output operations (<iostream>) and string operations (<string>), as well as the user-defined headers for the Person and Address classes.

  • using namespace std; allows the use of functions and classes from the std (standard) namespace without prefixing them with std::.

  • The Person constructor (Person::Person(string first, string middle, string last, Address* homeAdd)) sets the private attributes of a Person object based on the provided arguments. This constructor takes three strings for the person's first, middle, and last names, and a pointer to an Address object for the person's home address.

  • The print method prints the Person object's first, middle, and last names, and then uses the print method of the Address object to print the home address.

  • Setter methods (setName, setFirstName, setMiddleName, setLastName) are implemented to modify the respective private attributes of the Person object.

  • Getter methods (getFirstName, getMiddleName, getLastName, getAddress) are implemented to return the values of the respective private attributes of the Person object. The getAddress method returns a copy of the Address object pointed to by the homeAddress pointer.

Driver Program

//  Created by Kevin Roark on 6-15-23
// Driver to demo the Person Class and Address class - Aggregation

#include <iostream>
#include "Person.hpp"

using namespace std;

int main()
{
    Address home("1 Donore Square", "San Antonio", "Texas", "78229");
    Person myPerson("Kevin", "Read", "Roark", &home);
    myPerson.print();
    cout << endl;
    return 0;
}

This C++ program demonstrates the use of the Person and Address classes which were defined in earlier provided code snippets. This is typically called a "driver" program because it drives and tests the functionality of the classes.

Here's what the program does:

  1. It includes the necessary header file (Person.hpp), which in turn includes the Address.hpp header file.

  2. using namespace std; allows the use of functions and classes from the std (standard) namespace without prefixing them with std::.

  3. It defines a main function, which is the entry point of the program.

  4. Inside main:

    • An Address object named home is created with the values "1 Donore Square", "San Antonio", "Texas", and "78229" for street, city, state, and zip respectively.

    • A Person object named myPerson is created with the values "Kevin", "Read", "Roark" for first name, middle name, and last name respectively, and a pointer to home for the address. Note that the & operator is used to get the address of home because the Person constructor expects a pointer to an Address object.

    • The print method of myPerson is called, which prints the person's name and address to the console.

    • cout << endl; prints a newline character to separate the output from the prompt when the program finishes execution.

    • return 0; indicates that the program has finished execution successfully. It's a convention in C++ and many other programming languages to return 0 from the main function to indicate success.

The overall purpose of this program is to demonstrate how the Person and Address classes can be used together to represent and manipulate complex data.

  • No labels