02.OBJECT ORIENTED DESIGN & C++

SYNTAX

class className: memberAccessSpecifier baseClassName
{
  memberList
};
  • the memberAccessSpecifier is either public, protected, or private (default)

    • private members of the base can't be directly accessed by members of the derived class

    • public members of a base class can be inherited either as public or private members by the derived class

      • the derived class can include data/functions

DEFINITIONS

INHERITANCE

  • an object oriented design concept which relates many classes together to allow specific classes using members of general classes

  • a kind of hierarchy relation between different classes of different levels

OVERRIDING

  • occurs when public functions of the base class are redefined in a derived class (subclass)

  • the redefined function in the subclass must have the same name, number, and types of parameters (formal parameter list)

Overriding Sample
class Circle
{
  double selfRadius;
  void print();
};

void Circle::print(){
  cout << "Radius: " << selfRadius << "\n";
}

class Cylinder: public Circle
{
  double selfHeight;
  void print();                  //overriding the print function of the class circle
};

void Cylinder::print(){
  cout << "\nCall of print from class Cylinder.\n";
  
  //call of print of Circle class
  Circle::print();
}

OVERLOADING

  • occurs when both functions in the base class & in the derived class have the same name, but different formal parameter list (overloading is allowed in C++)

Overloading Sample
class Circle
{
  void get(double&);
};

void Circle::get(double& r){
  r = radius
}

class Cylinder: public Circle{
  void get(double&, double&);    //OVERLOADING
};

void Cylinder::get(double& r, double& h){
  Circle::get(r);
  h = height;
}

COMPOSITION

  • another way to relate two or more classes - similar to inheritance

  • used for objects that have a "has-a" relationship to each other

  • C++ allows object composition by using classes as member variables in other classes

POLYMORPHISM

  • this is an OOD principle

  • can be discussed via overloading or via templates

  • allows the programmer defining operators/functions w/ same name, but different formal parameter list

    • see slide 14 of chapter 2

TEMPLATES

  • enables the programmer to write generic codes for related functions & classes

  • enable the user writing a single code segment for a set of related functions (function template) or related classes (class template)

Template Syntax
template <class Type>
function definition;
Class Template Syntax
template <class Type>
class declaration;

INHERITANCE IMPLEMENTATION: CIRCLE

output of a C++ program that uses inheritance

UML

SOURCE CODE

HEADER FILE: circle.h

/*################################################
# Name: Stephen Razon
# Student ID: 
# Date: 170741SEP24
# Filename: circle.h
# Dependency: circleImplementation.h
################################################*/

const double PI = 3.141592653589793238463;

class Circle
{
  private:
    double selfRadius;
  public:
    Circle(double radius);
    double circumference();
    double area();
};

HEADER FILE: circleImplementation.h

/*#######################################
# Name: Stephen Razon
# Student ID: 
# Date: 170741SEP24
# Filename: circleImplementation.h
# Dependency: circle.h
#######################################*/

#include "circle.h"       //this links the circle.h to implementation.h

Circle::Circle(double radius){
  selfRadius = radius;
}

double Circle::circumference(){
  return 2 * PI * selfRadius;
}

double Circle::area(){
  return PI * selfRadius * selfRadius;
}

//inheritance; inherit the circle class
class Cylinder: public Circle            //cylinder is the child class & circle is the base class
{
  private:
    double selfHeight;
  public:
    Cylinder(double radius, double height);     //the 1st double is for radius which is in circle
                                         //the 2nd double is for height which is in cylinder
    double volume();
};

Cylinder::Cylinder(double radius, double height):Circle(radius){ //cylinder constructor w/ circle constructor
  selfHeight = height;
}

double Cylinder::volume(){
  return area() * selfHeight;
}

SOURCE FILE: inheritance.cpp

/*#######################################
# Name: Stephen Razon
# Student ID:
# Date: 170741SEP24
# Filename: inheritance.cpp
# Dependency: circleImplementation.h
#######################################*/

#include <iostream>
#include "circleImplementation.h"

using namespace std;

int main()
{
  Circle circ(3.7);
  cout << "Circle circumference: " << circ.circumference();
  cout << "\nCircle area: " << circ.area();

  Cylinder cyl(2.5, 6.2);            //1st arg is radius, 2nd arg is height
  cout << "\nCylinder volume: " << cyl.volume() << "\n";

  return 0;
}

COMPOSITION IMPLEMENTATION: PERSON

output of a C++ program that uses classes & composition

SOURCE CODE

HEADER FILE: date.h

/*################################################
# Name: Stephen Razon
# Student ID:
# Date: 170741SEP24
# Filename: date.h
# Dependency: N/A
################################################*/

class Date
{
private:
    int selfMonths, selfDays, selfYears;
public:
    Date(int month, int day, int year);            //constructor
    void printDate();
    void setDate(int month, int day, int year);    //setter function uses pass by value
    void getDate(int& month, int& day, int& year); //getter function uses pass by reference
};

Date::Date(int month, int day, int year) {
	selfMonths = month;
	selfDays = day;
	selfYears = year;
}

void Date::printDate() {
	cout << selfMonths << "/" << selfDays << "/" << selfYears << "\n";
}

void Date::setDate(int month, int day, int year) {
	selfMonths = month;
	selfDays = day;
	selfYears = year;
}

void Date::getDate(int& month, int& day, int& year) {
	month = selfMonths;
	day = selfDays;
	year = selfYears;
}

HEADER FILE: person.h

/*################################################
# Name: Stephen Razon
# Student ID:
# Date: 170741SEP24
# Filename: person.h
# Dependency: N/A
################################################*/

class Person
{
private:
    string selfFirstName, selfLastName;
public:
    Person(string firstName, string lastName);         //constructor
    void printName();
    void setName(string firstName, string lastName);   //setter functions uses pass by value
    void getName(string& firstName, string& lastName); //getter function uses pass by reference
};

Person::Person(string firstName, string lastName) {
	selfFirstName = firstName;
	selfLastName = lastName;
}

void Person::printName() {
	cout << "Full Name: " << selfFirstName << " " << selfLastName << "\n";
}

void Person::setName(string firstName, string lastName) {
	selfFirstName = firstName;
	selfLastName = lastName;
}

void Person::getName(string& firstName, string& lastName) {
	firstName = selfFirstName;
	lastName = selfLastName;
}

HEADER FILE: personInfo.h

/*################################################
# Name: Stephen Razon
# Student ID:
# Date: 170741SEP24
# Filename: personInfo.h
# Dependency: personInfo.h, date.h
################################################*/

#include "person.h"
#include "date.h"

class personalInfoType
{
private:
    Person selfFullName;      //this is a composition of Person class - from person.h
    Date selfBirthDate;       //this is a composition of Date class - from date.h
    int selfId;
public:
    personalInfoType();       //default constructor
    //1st arg = firstName 2nd arg = lastName 3rd arg = month
    //4th arg = day 5th arg = year 6th arg = ID
    personalInfoType(string, string, int, int, int, int); //parameterized constructor

    void printPersonalInfo();
    void setPersonalInfo(string, string, int, int, int, int);
};

personalInfoType::personalInfoType() : selfFullName("NoName", "NoName"), selfBirthDate(0, 0, 0) {
	selfId = 0;
}

//1st arg = firstName 2nd arg = lastName 3rd arg = month
//4th arg = day 5th arg = year 6th arg = id
personalInfoType::personalInfoType(string firstName, string lastName, int month, int day, int year, int id) :selfFullName(firstName, lastName), selfBirthDate(month, day, year) { //parameterized constructor
	selfId = id;
}

void personalInfoType::printPersonalInfo() {
	cout << "Full Name: ";
	selfFullName.printName();        //printName is in personImplementation.h

	cout << "Date of Birth: ";
	selfBirthDate.printDate();      //printDate is in dateImplementation.h
	cout << "ID #: " << selfId << "\n";
}
void personalInfoType::setPersonalInfo(string firstName, string lastName, int month, int day, int year, int id) {
	selfFullName.setName(firstName, lastName);
	selfBirthDate.setDate(month, day, year);
	selfId = id;
}

SOURCE FILE: implementation.cpp

/*################################################
# Name: Stephen Razon
# Student ID:
# Date: 170741SEP24
# Filename: implementation.cpp
# Dependency: personInfo.h
################################################*/

#include <iostream>
#include "personInfo.h"

using namespace std;

int main()
{
	personalInfoType person1("John", "Smith", 2, 28, 2000, 1001), person2;
	person1.printPersonalInfo();

	person2.setPersonalInfo("Sara", "William", 3, 3, 2005, 1002);
	person2.printPersonalInfo();

	return 0;
}

TEMPLATE IMPLEMENTATION

output of a C++ program that uses templates & polymorphism

SOURCE CODE

HEADER FILE: template.h

/*################################################
# Name: Stephen Razon
# Student ID:
# Date: 170741SEP24
# Filename: template.h
# Dependency: N/A
################################################*/

template <class Type>
class Addition
{
private:
    Type x, y;
public:
    Addition(Type, Type);     //parameterized constructor
    Type addition();
};

//this is the constructor section
template <class Type>
Addition<Type>::Addition(Type xx, Type yy) {
	x = xx;
	y = yy;
}

//this is the implementation function
template <class Type>
Type Addition<Type>::addition() {
	return x + y;
}

SOURCE FILE: implementation.cpp

/*################################################
# Name: Stephen Razon
# Student ID:
# Date: 170741SEP24
# Filename: implementation.cpp
# Dependency: template.h
################################################*/

#include <iostream>
#include "template.h"

using namespace std;

int main()
{
	Addition<int> integerVar(4, 5);
	cout << integerVar.addition() << "\n";

	Addition<char> characterVar('a', 'b');
	cout << characterVar.addition() << "\n";

	Addition<string> stringVar("I am ", "Happy");
	cout << stringVar.addition() << "\n";

	Addition<float> floatVar(2.1, 3.2);
	cout << floatVar.addition() << "\n";

	return 0;
}

Last updated