OOP Lecture Introduction 1
OOP Lecture Introduction 1
N aveed Jhamat
• Course Objectives
• Objective of this course is to make students familiar with the concepts of object-oriented
programming
• Concepts will be reinforced by their implementation in C++
• Course Contents
• Object-Oriented Approach / Programming
• Objects and Classes
• Constructor, Copy Constructor, Destructor and Constructor Overloading
• Inheritance and Function Overriding
• Operator Overloading
• Friend Functions and Template Functions
• Virtual Functions and Polymorphism
• File Handling / Exception Handling
• Books
• ”C++ How to Program” by Deitel & Deitel
• “Object Oriented Programming in C++” by Robert Lafore.
What is Class?
void setY()
{
}; cin>>y;
}
void getX()
{
cout<<x;
}
void getY()
{
cout<<y;
}
};
What is Class … further concepts?
• When a class is defined, it does not occupy any space in the computer memory.
• “class” is a keyword.
• Object of the class will always be created from outside of the class.
• Class’s member functions can be defined inside the class definition or outside the class
definition.
• Class in C++ are similar to structures in C, the only difference being, class defaults to private
access control, where as structure defaults to public.
• All the features of OOP, revolve around classes in C++. Inheritance, Encapsulation,
Abstraction etc.
• Objects of class holds separate copies of data members. We can create as many objects of a
class as we need.
• Encapsulation / Information Hiding / Data hiding / Abstraction.
• OOP allows the programmers to hide the important data / details from the user (this is Abstraction /
data hidding). The process of providing only the essentials and hiding the details is known as
abstraction. It is implemented / performed by Encapsulation through code. Declaring the data
members private and accessing them through the public member functions is referred as
Encapsulation.
What is an object?
• The variable whose datatype is a class; / instance of a class / … Specific
• Any entity in the real word that may be Tangible ( Farhan, car etc ) or intellectually
apprehended (time, date etc)
• An object has:
1) State / properties / characteristics / attributes / data
• set of values of the attributes of a particular object is referred as its state.
2) Behaviour / operations / methods / actions / functions
• set of all functions of a particular object is referred as its behavior.
3) Identity
4) Surrounding / Relationship
• An object represents the data members of a class in the memory
• Each object of the class has unique name
• The name of the object differentiate it from other objects of the same class
• How the object is created / declared : … just like declaring variable e.g. int x;
Class_Name Object_Name; e.g. Student ali; or… Test obj;
Syntax Explanation Data Identifier Concept
Type
public:
obj.setX(); // Its ok; public member function
void setX()
{
cin>>x;
}
void getX()
{
cout<<x;
} getch();
};
};
#include <iostream>
using namespace std;
class Student
{
private: int main()
char name[10];
int roll_No;
{
int m1, m2, m3;
int total, average;
protected:
public:
void set_Data()
{ Student S1, S2, S3;
cout<<"Enter student name::"; cin>>name;
cout<<"Enter roll no of the student::";cin>>roll_No;
cout<<"Enter marks of first subject::"; cin>>m1; S1.set_Data();
cout<<"Enter marks of second subject::"; cin>>m2;
cout<<"Enter marks of third subject::"; cin>>m3;
S1.get_Average();
}
void get_Average()
S2.set_Data();
{ S2.get_Average();
cout<<"student name::” << name <<endl;
cout<<"roll no::" << roll_No <<endl;
}
};
Assignment:-
1. Copy the source code on slides 13 and 14 on the word document;
write in detail your comments against each statement.
2. Write a program that will display the result card of a student: a
result card will contain marks of five subjects and display the GPA of
the student in the first semester.
3. First understand, then execute and then write the source code of
first five examples of your IT book along with your comments on
each statement.
4. Write a program that will display the result card of a three students:
a result card will contain marks of five subjects in each semester
and display the CGPA of the students in their three semester.
Assignment…
• 5. Define a class student with the following specification
Private members of class student
admno integer
sname 20 character
eng. math, science float
total float
ctotal() a function to calculate eng + math + science with float return type.
Public members:
readdata() Function to accept value from bcode, name, innings, notout and invoke the function
calcavg()
displaydata() Function to display the data members on the screen.
• Computer Science A Structured Programming Approach Using C++
Written by:- Behrouz A. Forouzan