You are on page 1of 34

CSEE2123: OOP and Data Structures

Fall 2018
Object Oriented Programming(OOP),
Classes
Lecture 5
M. Tahir Awan
(mtahir@cust.edu.pk)
Capital University of Science & Technology (CUST),
Islamabad
Structure in C\C++
• A structure is a collection of data values, called
data members, that form a single unit.
• Members of a structure can be of different types
• We can create new data types using structures

• Examples
struct complexNumber{
struct student{ float real;
char Name[20]; float complex;
float CGPA; };
};
struct book{
char title[30];
int price;
};
10/2/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 2
Declaration of Structure type Variables
• Structure variables are declared like other
variables

struct student{
struct student{
char Name[20]; char Name[20];
float CGPA; float CGPA;
}; }s1, s2;
struct student s1, s2; Alternate Declaration Method
struct student *ptrs;

• Structure Members are local to the structure.


Member names are not known outside the
structure.
10/2/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 3
Structure and pointers
• Pointers can also be used with structures by
defining a pointer variable of type struct.

struct distance{
distance d1; float feet;
distance *ptr = &d1 float inches;
};
ptr-> feet = 10;
ptr-> inches = 5;
(*ptr).inches = 5;

• Arrow operator (->) is used with struct type


pointers to access its members.

10/2/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 4


Using Structures With Functions
• Passing structures to functions
• Pass structures by Value
– Pass entire structure as function argument
– Changes made to struct members will not be
visible outside function
• Pass structures by Reference
– Pass a pointer to structure i.e. its address
– Changes made to structure members will
modify original structure
• Return Value as structure
– Function can return a structure

5
10/2/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST
C++ : User-Defined Data Types

User-Defined Data
Types

enum struct union class


Object Oriented Programming(OOP)
Programming Paradigms
• In computer programming following paradigms
exist :
–Procedural Programming
» C, Fortran, Pascal
–Object Oriented Programming
» C++, Java, Python

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 8


Procedural Programming Paradigm
• Procedural programming uses a series of
computations to solve a problem in
sequential manner
• Procedural programming is based on
procedures (functions)
• Procedural programming uses a top-
down approach to solve a problem
• e.g. C, Fortran, Pascal

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 9


Procedural Programming Paradigm
• Procedural Programming uses procedures/
functions and global, local variables, structures
to communicate and store data

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 10


C Programming Language
• C-Language is procedural programming
language that contain :
– functions, which represent behavior
– variables, which represent data
– structures, which represent categories of data
• C Programs are based on functions

C Program

CSEE1133:Data Structures © M. Tahir Awan, CUST 11


Procedural Programming : Issues
• In procedural programming paradigm,
–Data is exposed to all functions in the
program (No Security)
–Larger programs become difficult to
understand (spaghetti-code)
–Basis used provide a poor model of the
real world
• OOP paradigm makes it easier to organize
variables and functions

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 12


Object Oriented Programming (OOP)
• In Object-oriented programming all
computations are done using objects that can
also interact/communicate with other elements
• Object-oriented programming approach
organizes programs in the form of objects that
can mirror real world objects, that are associated
with both attributes and behavior
• Object-oriented programming bundles
procedures and data into a single entity called
objects
• OOP Languages : C++ , Java, Python

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 13


Real World Objects
• Real world is composed of objects of
different types like cars, people, animals,
machines
• A real world object is represented by its
– Attribute : characteristics of the object
– Behavior : how real world objects will behave
to outside stimulus
• A Car has four wheels, steering, brakes
(attributes).
• If we move the steering towrards left or right, car
will turn. If we press brakes , car will stop
(behavior )
CSEE1133:Data Structures © M. Tahir Awan, CUST 14
Object Oriented Paradigm
• Fundamental idea in
object oriented
programming is to
model entities like
real world objects
• An OOP program can
be viewed as a
collection of
cooperating objects

CSEE1133:Data Structures © M. Tahir Awan, CUST 15


OOP : Classes
• Classes are templates that define objects of the
same type in a software program
• A class uses
– variables to define\store data fields (attribute)
– functions to define its behaviors
• A class combines both data and functions into a
single unit
• Functions of a class operates on its variables
• A class is a template from which multiple
objects can be created

CSEE1133:Data Structures © M. Tahir Awan, CUST 16


OOP : Object
• An object is an instance of some class
• An object is represented by its
– Attribute : object’s data that is stored in
variables
– Behavior : Functions inside object represent
its behavior
• An OOP program typically consists of a number
of objects, that communicate with each other by
calling one another’s member functions
• Classes are models (e.g. map of a house) while
objects deal with data (actual house)

CSEE1133:Data Structures © M. Tahir Awan, CUST 17


Features of Object Oriented
Programming
• Features of OOP are :
–Encapsulation or Data hiding
–Inheritance and Reusability
–Polymorphism
–Templates and Template Classes

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 18


Features of Object Oriented
Programming
• Encapsulation or Data hiding
• Ability of an object to hide its data and methods
from the rest of the world. Encapsulation is one
of the fundamental principles of OOP
• Encapsulation insures data security
• Inheritance and Reusability
• Object oriented programming languages allow
classes to inherit commonly used attributes and
behavior from other classes
• The child or derived class inherits all the
features of its parent or base class
• Inheritance helps in software reusability

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 19
Features of Object Oriented
Programming
• Polymorphism
• Polymorphism means one name, many forms.
Polymorphism manifests itself by having
multiple functions all with the same name, but
slight different functionality.
• Polymorphism has two types. Overriding and
overloading
• Templates and Template Classes
• Templates are a feature of the C++ that allow
functions and classes to operate with generic
data types. A function or class can work with
different data types without being rewritten

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 20


Definition of a ‘class’
• Definition of a class is composed of
• 1. Data Members
• The data items within a class are
called data members or data fields
that define its attributes
• 2. Function Members
• Member functions are included
within a class to define its behavior
• All computations on an object are
done using member functions

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 21


{private , public} Access Specifier
• Data encapsulation in OOP is done using access
specifiers
• Private (private)
• A private member (data or functions) can only be
accessed from inside class. Only the class and
friend functions can access private members.
• Public (public)
• A Public member (data or functions) can be
accessed from anywhere outside of class
• By default all the members of a class are private
• Mostly data members of class are made private
while function members are made public
10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 22
C++ : Class Definition
Definition of a class starts with the keyword ‘class’
followed by class name
Defining Objects, Access Member
• In main program objects of class are defined
also known as instantiating the objects
foo f1, f2;

• Calling Member Functions


• Public Members (data, functions) of class are
accessed using ‘.’ notation
f1.memfunc(10)
f2.memfunc(50)
• Private members cannot be accessed using ‘.’
notation

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 24


C++ Class Definition : Example
• Definition of Class and Object

#include <iostream> void main() {


using namespace std; Circle C1;
cout<<“Area of circle = “ <<
class Circle { C1.getArea();
private: }
float radius;

public:
Circle() {
radius = 5.0;
}
float getArea() {
return radius* radius* 3.14159;
}
};

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 25


Class Definition :
Member functions Out-side class
• Member functions of class can be defined out-
side of class definition
float Circle::getArea() {
return radius* radius* 3.14159;
}

• Scope resolution operator ::


• Class Name :: function Name

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 26


C++ Class Definition 2: Example
• Member Function Definition outside of class

#include <iostream> Circle::Circle() {


using namespace std; radius = 5.0;
}
class Circle {
private: float Circle::getArea() {
float radius; return radius* radius* 3.14159;
}
public:
Circle() ; void main() {
float getArea(); Circle C1;
}; cout<<“Area of circle = “ <<
C1.getArea();
}

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 27


Special Function : Constructor
• A class constructor is a special member function
of the class that is executed automatically
whenever new object of that class is created
• Constructor will have exact same name as the
class and it does not have any return type at all,
not even void
• Constructors are useful for setting initial values
for certain member variables
• A class may be declared without a constructor.
In this case, a no-argument constructor with an
empty body is implicitly declared known as
default constructor

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 28


Special Function : Destructor
• A class destructor is a special member function
of the class that is executed automatically
whenever an object of it's class goes out of
scope
• Destructor will have exact same name as the
class prefixed with a tilde (~) and it can neither
return a value nor can it take any parameters.
~circle () {};
• Destructor are useful for releasing resources
before coming out of the program like closing
files, releasing dynamically allocated memories

• Constructor and destructor are never explicitly


called in the program
10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 29
C++ Class Definition : Example
• Definition of Class and Object

#include <iostream> float getArea() {


using namespace std; return radius* radius* 3.14159;
}
class Circle { };
private:
float radius; void main() {
Circle C1;
public: cout<<“Area of circle = “ <<
C1.getArea();
Circle() {
}
radius = 5.0;
}
~Circle() {
cout<<“Object out of scope”;
}

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 30


C++ Point Class : Example
• Definition of Class and Object

#include <iostream> int getY() {


using namespace std; return y;
};
class Point {
private: void main() {
int x; Point p1;
int y; cout<<“Coordinates of Point are
( “ << p1.getX
<<“,”<<p1.getY()<<endl;
public:
}
Point() {
x = 2;y=3;
}

int getX() {
return x; }
};

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 31


C++ Class : Get & Set Functions
• To access private members of a class in outside
world, get and set methods are used. A get
function is referred to as a getter (or accessor),
and a set function is referred to as a setter (or
mutator).

• Get function will return value of member


int getradius(){ return radius; }

• Set function will modify value of member


void setradius (int r){ radius = r; }

10/2/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 32


C++ Class : Complex Number
• Definition of Class and Object

#include <iostream> Complex::Complex(float real, float


using namespace std; imag)
: real(real), imag(imag) { }
class Complex {
private: float Complex::getReal() {
float real; return real;
float imag; }

public: void Complex::setReal(float r) {


Complex(float real = 0.0, float real = r;
imag = 0.0); }
float getReal();
void setReal(float real); float Complex::getImag() {
float getImag() ; return imag;
void setImag(float imag); }
};

CSEE1133:Data Structures © M. Tahir Awan, CUST 33


C++ Class : Complex Number
• Definition of Class and Object

void Complex::setImag(float im) { cout << "C2:Real Part = "


imag = im; <<c2.getReal() <<", Imaginary
Part = "<<c2.getImag()<<endl;
}

}
int main() {
Complex c1, c2(4, 5);

c1.setReal(0);
c1.setImag(8);

cout << "C1:Real Part = "


<<c1.getReal() <<", Imaginary
Part = "<<c1.getImag()<<endl;

CSEE1133:Data Structures © M. Tahir Awan, CUST 34

You might also like