0% found this document useful (0 votes)
19 views

OOP Lecture Introduction 1

The document outlines an object-oriented programming course, including its objectives, contents, and recommended books. The course objectives are to make students familiar with OOP concepts and their implementation in C++. The course contents include topics like classes, objects, inheritance, polymorphism, and file handling. The recommended books are "C++ How to Program" by Deitel & Deitel and "Object Oriented Programming in C++" by Robert Lafore.

Uploaded by

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

OOP Lecture Introduction 1

The document outlines an object-oriented programming course, including its objectives, contents, and recommended books. The course objectives are to make students familiar with OOP concepts and their implementation in C++. The course contents include topics like classes, objects, inheritance, polymorphism, and file handling. The recommended books are "C++ How to Program" by Deitel & Deitel and "Object Oriented Programming in C++" by Robert Lafore.

Uploaded by

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

OOP Course Outline 3rd Semester BSIT / BSCS by Dr.

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?

• UDT / ADT / Concept / Template / Category / Prototype…/ Generic

I. Class is a user defined data type (UDT)


II. Class consists of members of the class
• Combination of 1) data members and 2)member functions
 these Member Functions will manipulate the Data Members
III. Class will be defined under three Access Specifiers
• 1) private 2) protected 3) public
What are Access Specifiers?
1. These are the commands which specify / determine the access of members of the class (data
members and member functions)
2. Whether these members of the class can be accessed from outside the class OR not; (by the
object of that class).
3. Normally, there is no significant difference between private and protected; but this difference
matters in inheritance.
4. The “private members” of the class can be accessed only from / within the class ( i.e. can not be
accessed from outside the class).
5. The “public members” of the class can be accessed BOTH from inside and from outside the class.
6. Only public members of the class can be accessed outside by objects of the class with the help of
dot operator / access operator (.)
7. Private and protected members of the class can be accessed through the public member
functions that will be accessed by objects of the class with the help of dot operator / access
operator (.)
8. Normally, data members are declared as private and member functions are declared as public;
but we can also declare data as public and functions as private.
9. If no access specifier is given / written in the class, the members are treated as private (means by
default private).
10. After the dot operator, there will always be the public member of the class.
Declaring / Defining a class / Structure of a class? Example…
class Test
class class_Identifier {
{ private:
int x;
private: float y;
protected:
public:
int z;
protected: void setX()
{
cin>>x;
public: }

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

int x; Variable declaration int x x is a variable,


whose data type is int

Student ali; Object declaration


Or
Student ali Ali is an object,
whose data type is Student
Object creation Student is user defined data type
(class)
#include <iostream.h> void main ()
#include <conio.h>
{
class Test clrscr();
{
private: Test obj; // “just like” int abc;
int x; // object is created; occupy memory
float y;
protected:
public: obj.x= 10; // error; private data member
int z; obj.y= 20; // error; private data member
void setX()
{
cin>>x; obj.z= 30; // Its ok; public data member
}
obj.setX(); // Its ok; public member function
void setY()
{ obj.setY(); // Its ok; public member function
cin>>y;
}
void getX() obj.getX(); // Its ok; public member function
{
cout<<x;
} obj.getY(); // Its ok; public member function
cout<< obj.z ; // Its ok; public data member
void getY()
{
cout<<y;
} getch();
}
};
• Summary
• Classes are the most important feature of C++ that leads to Object Oriented programming.
Classes in C++ are user defined data types, which hold multiple variables(data members) of different types
and functions(member functions) that access & perform operations on those variables. When we define any
class, we are not defining any data, we just define a structure or a blueprint, as to what the object of that
class type will contain and what operations can be performed on that object.
• Objects in simple terms are variables of type class, so classes can be considered as data types(user defined)
of these variables(objects). Objects are instances of class, which holds the data variables declared in class
and the member functions work on these class objects.
Each object has different data variables.
• Summary…
• The keyword public / private / protected determines the access
attributes of the members of the class that follow it. A
private member cannot be directly accessed by the object and in
order to access the data members, you have you have to define
member functions which generally have public access specifier. You
can also the data members and member functions as public, private
or protected.
• A class provides the blueprints for objects, so basically an object is
created from a class. We declare objects of a class with exactly the
same sort of declaration that we declare variables of basic types.

• Another example program…


#include <iostream>
#include <cstring> int main( )
using namespace std; {
class Box Box b1;
{
private: //setting length, breadth and height
int length; // Length of a box b1.setData(10,6,5);
int breadth; // Breadth of a box
public: //accessing private members using member functions
int height; // Height of a box cout<<"Length of Box: "<<b1.getLength()<<endl;
void setData(int l, int b,int h) cout<<"Breadth of Box: "<<b1.getBreadth()<<endl;
{
length=l; // accessing public member height using (.) operator
breadth=b; cout<<"Height of Box: "<<b1.height<<endl;
height=h;
} return 0;
int getLength()
{return length;}
int getBreadth() }
{return breadth;}
};
• Explanation of the program
• We first defines the Box class using the class keyword. Inside the class we
created 3 data members. the length and breadth are private while the height is
public. We then defined 2 member functions : setData to set the values of the
data members and getLength, getBreadth to retrieve those values. Since height
is public member, we didn’t have to create a member function to retrieve its
value and we can use a (.) operator directly to set or get its value. In the main()
function we first created an object(b1)of Box type. We then assigned values to
its data members using the setData function. Lastly we accessed the values of
length and breadth using getLength and getBreadth respectively and for
accessing the value of height we simply used the (.)operator.
• You may have noticed two keywords: private and public in the above example.
The private keyword makes data and functions private. Private data and
functions can be accessed only from inside the same class.
• The public keyword makes data and functions public. Public data and functions
can be accessed out of the class.
• Here, length and breadth are private members & height is public data members
where as all functions are public.If you try to access private data from outside
of the class, compiler throws error. This feature in OOP is known as data hiding.
#include <iostream.h> void main ()
#include <conio.h>
{
class Test clrscr();
{
private: Test obj; // “just like” int x;
int x; // object is created; occupy memory

public:
obj.setX(); // Its ok; public member function
void setX()
{
cin>>x;
}

obj.getX(); // Its ok; public member function

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;

cout<<"marks of first subject:: ” << m1


cout<<"marks of second subject:: " << m2
<<endl;
<<endl;
S3.set_Data();
cout<<"marks of third subject:: " << m3 <<endl; S3.get_Average();
total=m1+m2+m3;
average=total/3; }
cout<< "Average marks of " <<name<< " will be " <<average;

}
};
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 member function of class student


Takedata() Function to accept values for admno, sname, eng, science and invoke ctotal() to calculate total.
Showdata() Function to display all the data members on the screen.
• 6. Define a class batsman with the following specifications:
Private members:
bcode 4 digits code number
bname 20 characters
innings, notout, runs integer type
batavg it is calculated according to the formula – batavg =runs/(innings-notout)
calcavg() Function to compute batavg

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

You might also like