Object Oriented Programming Techniques
Object Oriented Programming Techniques
Techniques
Programming Paradigm
Procedural Programming
• Structured/Modular Programming
• Object-Oriented Programming
▪ In OOP, the first step in the problem-solving process is to identify the
components called objects, which form the basis of the solution, and to
determine how these objects interact with one another
▪ For example, suppose you want to write a program that automates the
University Management System
10
Introduction to the Class (Cont..)
11
Access Specifiers
● The key words private and public are access specifiers.
● private means they can only be accessed by the member functions
● public means they can be called from statements outside the class
○ Note: the default access of a class is private, but it is still a good idea to use the
private key word to explicitly declare private members
○ This clearly documents the access specification of the class
12
Example:
class Rectangle
{
private:
float width, length, area;
public:
void setData(float, float);
void calcArea(void);
float getWidth(void);
float getLength(void);
float getArea(void);
};
13
Defining an Instance of a Class
• Class objects must be defined after the class is declared.
• Defining a class object is called the instantiation of a class.
• Rectangle box; // box is an instance of Rectangle
14
Objects and Classes
#include <iostream>
class smallobj //define a class
{
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{ somedata = d; }
void showdata() //member function to display data
{ cout << “Data is “ << somedata << endl; }
};
15
Objects and Classes Cont..
int main()
{
smallobj s1, s2; //define two objects of class smallobj
s1.setdata(1066); //call member function to set data
s2.setdata(1776);
s1.showdata(); //call member function to display data
s2.showdata();
return 0;
}
16