Chapter - 5 - Introduction To Object Oriented Programming
Chapter - 5 - Introduction To Object Oriented Programming
Introduction to Object
Oriented Programming
1
Objectives
At the end of this chapter we aim students
● Understand Definition of Basic OO programming
Concepts (Abstraction, encapsulation, inheritance and
Polymorphism)
● Understand the concepts of classes and objects
● Define Classes
● Instantiate classes and Object variables
● Understand Constructor Methods
● Define Derived Classes (Inheritance)
● Understand Method Overloading and
Overriding(redefining)
Example:
#include<iostream>
usign namespace std;
struct Account {
void main(){
int num;
Account saving;
double balance;
saving.num=100;
void display(){
saving.balance = 23456.00
cout<<“Account N0.: “<<num;
saving.display(); }
cout<<“\nBalance: “ <<balance; }
};
#include<iostream>
usign namespace std;
struct Account {
int num; void main(){
double balance; Account saving;
void display(){ saving.num=100;
cout<<“Account N0.: “<<num; saving.balance = 23456.00
cout<<“\nBalance: “ <<balance; } saving.display(); }
}; Chapter 5:Introduction to Object oriented proramming 14
Member functions and data members
Functions declared in a class are called member functions
The member functions provide the interface to access the
variables in the class
The variables are called data members
class Account {
int num;
double balance;
void didsplay(){
cout<<balance;
}
};
Class
class Account {
int num; Objects
Account
double balance;
void didsplay(){ Data members
cout<<balance; }
Num cust1 cust2
Balance
};
Member function 001 002
int main(){ Display
Account cust1, cust2; 10000.15 10000.15
…. }
Chapter 5:Introduction to Object oriented proramming 17
Access Control
The access to the data members and member functions of a class or structure
can be controlled by using the access control key words or access specifiers:
Private
Public
The data members or member functions declared private can only be accessed
from within the class
the default access for structure is public and private for class
Objects outside the class can access its private data members only through the
member functions of the class that are declared public
The member functions cannot be accessed from outside if they are declared
private
Member functions have to be declared public for meaningful programs
The data members should not be declared public as it defeats the very
purpose of data hiding, which is one of the most important requirements
of object oriented programming
the member functions are usually declared public
Declaring both data members and member functions private will totally
shield the class from outside world and therefore serves no purpose out there