You are on page 1of 33

Object Oriented Programming

Dr. Jose Annunziato

Object Oriented Programming (OOP)
• OOP is a programming paradigm centered around objects • Objects represent instances of real objects or concepts in a particular domain • OOP formalizes the use of good programming practices such as
– data abstraction – messaging – polymorphism – reusability  encapsulation  modularity  inheritance  information hiding

Introduction to Classes
• Classes allow creating new data types and enforce good programming practices such as information hiding & reusability • We'll start with a simple class and introduce several features and their merit • Classes explicitly combine data and operations under the same abstraction • Information hiding can be enforced by declaring public and private class members

string address. int zip. . string state.Example of a Class • Variables and functions declared in a class are referred to as class members class Bank { public: string name. }.

Bank citiBank. Bank citizensBank.Creating Instance Objects • Classes define how data is represented and manipulated • Instances of a class are referred to as objects • Declare objects by declaring a variable of the class type Bank bankOfAmerica. .

cout << citiBank.state << endl.address << endl. citiBank.name = "Citi Bank".zip << endl. cout << citiBank. citiBank. . cout << citiBank.address = "321 Main St.". • Use dot notation to retrieve object member values cout << citiBank.name << endl.zip = 54321. citiBank.state = "NY".Using Instances • Use dot notation to change object members citiBank.

Classes • Objects have their own copy of the data members defined by the class of their type Bank name : string address : string state : string zip : int bankOfAmerica : Bank citiBank : Bank name = "Bank of America" address = "123 Broadway" state = "MA" zip = 12345 name = "Citi Bank" address = "321 Main St" state = "NY" zip = 54321 .Objects Vs.

10293 }. .Object Initialization • Objects can be initialized when they are declared Bank citizensBank = { "Citizens Bank".". "MA". "123 Main St.

int zip. }. void display(). .Declaring Member Functions • Classes can also declare member functions that can operate on data members class Bank { public: string name. string address. string state.

cout << "zip:"<< zip<< endl. cout << "city:"<< city<< endl. } . cout << "address: "<< address<< endl.Defining Member Functions • Member functions are defined using the class as a namespace void Bank::display() { cout << "name:"<< name<< endl. cout << "state:"<< state<< endl.

citiBank.display().Calling Object Methods • Objects have their own copy of their data members • Object instances of the same class share the function code declared in their classes • Use dot notation to invoke methods declared in an object's class Bank bankOfAmerica. . citizensBank. … bankOfAmerica.display().display(). citizensBank. citiBank.

~Bank ().Constructors and Destructors • Constructors and destructors are special types of functions that can manage object lifecycle • Constructors allow logically initializing an object when it is created • Destructors allow an object to clean up after itself class Bank { public: Bank (). } .

name = "Bank of America".Defining Constructors • Constructors are called when an object is declared int main() { Bank boa. boa. } • Constructors and destructors have no return types . } Bank::Bank() { cout << "Creating Bank" << endl.

boa.Defining Destructors • Destructors are called at end of object's lifecycle int main() { Bank boa. getch().name = "Bank of America". } Bank::~Bank() { cout << "Destroying " << name << endl. } .

i. string.e. they can take arguments • Arguments allow logically determining the initial state of an object • Constructors can make sure initial state is correct class Bank { public: Bank ( string. string. } .Parameterized Constructors • Constructors can be parameterized.. string. int ).

string adrs. address = adrs. } . zip = zp. city = ct.Parameterized Constructors • Often. string ct. constructors just provide initial values Bank::Bank ( string nm. state = st. string st. int zp ) { name = nm.

int zp = 12345 ) { name = nm. state = st. zip = zp. string adrs. string ct. constructor parameters can have default values Bank::Bank ( string nm. } . city = ct. address = adrs. string st.Default Parameter Values • Like for any function.

a default constructor is not defined for you .Default Constructors • Declaring constructor and destructors is optional • If a constructor is not declared. is default constructor • You can only have one default constructor • If you define any constructor. a default constructor is declared for you • A default constructor takes no arguments • A constructor with all default parameters.

} . } Bank::Bank() { cout << "I'm the default constructor" << endl.Declaring a Default Constructor class Bank { public: Bank ().

cpp • ConstructorDestructorDemo.ClassesDemo.cpp .cpp • ClassesDemo.

Bank. #endif . int zip. e.. display(). string. string address.Separate Declaration & Implementation • Declare classes in .g. string. string.h #ifndef BANK_H #define BANK_H #include … class Bank { public: string name. int ). }.h files. string state. Bank ( string.

e. cout << address << endl. address = adrs.cpp files. … } void Bank::display() { cout << name << endl.g. Bank.cpp • Define implementation in . string adrs.. … ) { name = nm.cpp #include "Bank.h" Bank::Bank ( string nm. … } .Define Implementation in .

Information Hiding • It is good practice to hide implementation details and data representation behind a well defined interface • An Application Programming Interface (API) • This allows implementations to change without affecting existing uses of your classes • Classes allow defining public and private parts • Hide implementation behind private and expose interface behind public .

string state. int zip. string city.Private Class Members • Hide data implementation behind private class Bank { private: string name. string address. … } .

~Bank(). void setName ( string name ). } . … display().Public Class Members • Expose data and operations with public class Bank { public: Bank(). string getName().

string getState(). … }. void setCity ( string ).Setters And Getters • Define setters and getters to mutate and access data members class Bank { public: string getName(). int getZip(). . void setZip ( int ). void setAddress ( string ). void setState ( string ). string getCity(). void setName ( string ). string getAddress().

… • Use setters (mutators) to modify (mutate) an object's data members. … .Setters and Getters private: string name. … • Use getters (accessors) to access an object's data members public: string getName(). string address. to mutate object's state public: void setName ( string name ).

validation of changes to object's state • Getters allow controlled access to data members. Setters. beyond just setting values • You can load an object from file. calculate some member variables. can calculate values on the fly . validation • Setters allow controlled changes to data members. Getters? • Constructors allow programmatic initialization of object's state.Why Constructors.

#endif . string parseCsvRow ( string row. int commaIndex ).Loading Objects From File • Declare CSV parsing library #ifndef PARSECSVROW_H #define PARSECSVROW_H #include <iostream> #include <conio.h> #include <string> using namespace std.

void save (). . … }.Bank Load & Save • Add load() and save() methods to Bank.h" class Bank { … public: void load ().h #include "ParseCsvRow.

} . 0 ).c_str() ). name = parseCsvRow ( bankStr. address = parseCsvRow ( bankStr. city = parseCsvRow ( bankStr. bankStr ). 2 ).Implement Load & Save • Implement load() and save() in Bank. 1 ). getline ( inputFile. state = parseCsvRow ( bankStr.csv".cpp void Bank::load () { string fileName = name + ". ifstream inputFile ( fileName ). string bankStr. 4 ). 3 ). zip = atoi ( parseCsvRow ( bankStr.

" << zip << endl. } . ofstream outputFile ( fileName )." << state << ".cpp void Bank::save () { string fileName = name + "." << city << ".Implement Load & Save • Implement load() and save() in Bank." << address << ".csv". outputFile << name << ".

cpp • LoadObjectsFromFileDemo.Demo • InformationHidingDemo.cpp • SeparateClassInterfaceAndImpl.cpp .