0% found this document useful (0 votes)
32 views31 pages

CSEE2123: OOP and Data Structures: Fall 2018

The document discusses object oriented programming concepts like classes, objects, encapsulation, inheritance and polymorphism. It provides examples of defining classes in C++ with data members, member functions, constructors and destructors. The examples demonstrate creating objects and accessing members through get and set methods.

Uploaded by

Umer Ghouri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views31 pages

CSEE2123: OOP and Data Structures: Fall 2018

The document discusses object oriented programming concepts like classes, objects, encapsulation, inheritance and polymorphism. It provides examples of defining classes in C++ with data members, member functions, constructors and destructors. The examples demonstrate creating objects and accessing members through get and set methods.

Uploaded by

Umer Ghouri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

CSEE2123: OOP and Data Structures

Fall 2018
Object Oriented Programming(OOP),
Classes
Lecture 6
M. Tahir Awan
([email protected])
Capital University of Science & Technology (CUST),
Islamabad
Assignment # 1 :
Programming Assignment
• Due Date :
– Friday, October 12th , 2018
– Due at : 05:00 p.m.
• Late Submission
– Not Allowed
• Assignment Submission
– Separate *.cpp file for each Question
– Submit in zip format
– File name : “Name_RollNo_AssignNo.zip”
– Submission Path
» \\fs\assignments$\mTahir\Assignment1

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


Dynamic Memory Allocation :
2 D Array
• To dynamically allocate 2D array
– Dynamically create array of pointers
– Dynamically create multiple 1D array
– Map 1D arrays to array of pointers

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


Dynamic Memory Allocation :
2 D Array
• Definition of Class and Object

#include <iostream> // Display the array


using namespace std; for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j)
void main() { cout << ary[i][j] << endl;
int N = 3;
int M = 3; // free the memory
for(int i = 0; i < N; ++i)
//Dynamic Memory Allocation delete [] ary[i];
int** ary = new int*[N]; delete [] ary;
for(int i = 0; i < N; ++i)
ary[i] = new int[M];
//Fill the array }
for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j)
cin>>ary[i][j];

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


C++ : User-Defined Data Types

User-Defined Data
Types

enum struct union class


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

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


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 7


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

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


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/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 9


{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/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 10
C++ : Class Definition
Definition of a class starts with the keyword „class‟
followed by class name
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/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 12


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/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 13


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/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 14


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/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 15


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/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 16
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/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 17


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/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 18


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/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 19


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 20


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 21


Class Constructor : Definitions
• Class Constructor can be defined in one of many
ways to initialize class attributes
Circle() : radius(0) {}
Circle()
{ radius = 0; }
Circle (int r) : radius(r)
{}
Circle (int r)
{ radius = r; }
Circle (int r = 1)
{ radius = r; }
10/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 22
C++ Classes : Practice Questions
• Define and instantiate objects of following
classes in C++ Program
• Distance Class
• Primitives : feet , inches of type float

• Time Class
• Primitives : hours, minutes, seconds type int

• Student Class
• Primitives : Name of type string, marks of type
int , grade of type char
10/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 23
Constructor Overloading
• In C++ multiple constructors can be defined
within a class. Concept is known as constructor
overloading
• Compiler will differentiate between multiple
constructors depending upon number of
arguments in constructor definition
• Constructor Overloading is used to increase the
flexibility in variable initialization
Complex() Complex( float r, float i)
{ {
real = 0.0; real = r;
imag = 0.0; imag = i;
} }

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


C++ Class : Distance
• Constructor Overloading

#include <iostream> void showdist(){


using namespace std; cout << feet << "-" << inches
<<endl;
class Distance { }
private: };
int feet; void main() {
float inches; Distance d1;
public: Distance d2(20, 5);
Distance():feet(0),inches(0) {}
Distance(int ft, float in) : d1.showdist();
feet(ft), inches(in) {} d2.showdist();
void setdist() { }
cout << "Enter feet: ";
cin >> feet;
cout << "Enter inches: ";
cin >> inches;
}
CSEE1133:Data Structures © M. Tahir Awan, CUST 25
Default Copy Constructor
• Default copy constructor is a one argument
constructor implicitly defined by the compiler
• Default Copy Constructor is used for copying
contents of one object into another

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


C++ Class : Distance
• Default copy constructor

#include <iostream> void showdist(){


using namespace std; cout << feet << "-" << inches
<<endl;
class Distance { }
private: };
int feet; void main() {
float inches; Distance d1(20, 5);
public: Distance d2(d1);
Distance():feet(0),inches(0) {}
Distance(int ft, float in) : d1.showdist();
feet(ft), inches(in) {} d2.showdist();
void setdist() { }
cout << "Enter feet: ";
cin >> feet;
cout << "Enter inches: ";
cin >> inches;
}
CSEE1133:Data Structures © M. Tahir Awan, CUST 27
Objects as Function Arguments
• Class objects can also be passed as arguments
in class member functions
• Syntax for passing objects as function
arguments is same as for other data types and
structures
void Distance::addDistance(Distance d2, Distance d3)
{
inches = d2.inches + d3.inches;
feet = 0;
if(inches >= 12.0)
{
inches -= 12.0;
feet++;
}
feet += d2.feet + d3.feet;
}
10/4/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 28
Objects as Function Arguments
void Distance::addDistance(Distance d2, Distance d3)

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


Returning Objects from Functions
• Functions can also return Class objects
• Syntax for returning objects from functions is
same as for variables of different types and
structures
Distance Distance::addDistance(Distance d)
{
Distance temp;
temp.inches = inches + d.inches;
temp.feet = 0;
if(temp.inches >= 12.0) {
temp.inches -= 12.0;
temp.feet++;
}
temp.feet += feet + d.feet;
return temp;
}

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


Practice Question
• Modify the complexNumber class and add a
number function „addComplexNumbers()‟ that
can add two complex numbers. Complex number
will be passed as argument to function and
function will return resultant complex number
• Implement a Time Class that will have three
members, Hours, Minutes and Seconds. Add a
constructor to initialize the members. Also add a
member function to add two time instants.

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

You might also like