0% found this document useful (0 votes)
22 views13 pages

Oop Lec1

Uploaded by

fairykomal993
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)
22 views13 pages

Oop Lec1

Uploaded by

fairykomal993
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/ 13

About Instructor

Mr. Fayez Afzaal


Lecturer
Department of Computer Science
COMSATS University Islamabad,
Sahiwal Campus, Pakistan
Email: [email protected]
Object-Oriented
Programming in C++

Lecture 1
Object-Oriented Programming
 Object-Oriented programming is a programming technique in which programs are written on
the basis of objects.
 An object is a collection of data and functions.
 Object may represent a person, thing or place in real world
 Object oriented programs are easier to learn and modify

 Object-Oriented programming is a powerful technique to develop software. It is used to


analyze and design the applications in terms of object.

 Some of the object-oriented languages that have been developed are:


• C++
• Java
• C#
• Python
Features of Object-Oriented Programming
 Following are some features of object-oriented programming
 Objects
 OOP provides the facility of programming based on objects. Object is an entity that consists of
data and functions.
 Classes
 Classes are designs for creating objects. OOP provides the facility to design classes for creating
different objects. All properties and functions of an object are specified in classes.
 Real-world Modeling
 OOP is based on real-world modeling. As in real world, things have properties and working
capabilities. Similarly objects have data and functions. Data represents properties and functions
represent working of objects.
 Reusability
 OOP provides ways of reusing the data and code. Inheritance is a technique that allows a
programmer to use the code of existing program to create new programs.
 Polymorphism
 Polymorphism is an ability of an object to behave in multiple ways
Objects
 An object represents an entity in real world such as person, thing etc.
 An object is identified by its name. An object consists of the following two things:

 State: States are the characteristics of an object


 Behavior: It is like a function that can be performed by an object
 Example: Your desktop lamp may have only two possible states (on and off) and two possible
behaviors (turn on, turn off)
 Behavior or function is used to change the state of an object

 Examples of objects
 Physical Objects
o Vehicle such as car, bus, truck etc.
 Elements of the computer-user environment
o The mouse and keyboard
 Data-storage constructs
o Stack, Linked lists and Binary trees etc.
 Human entities
o Employees, Students and Customers etc.
 User-defined data types
o Time and Angles etc
A car object Example
 Attributes of object car
 The characteristics of an objects are known as its properties or attributes
 Each object has its own properties. These properties can be used to describe the object
 The attributes of an object Car can be as follows:
• Color
• Price
• Model
• Engine power
• Current gear
 The set of values of the attributes of a particular object is called its state
 Functions of object car
 An object can perform different tasks and actions. The actions that can be performed by an
object are known as functions or methods
 The functions of a car object as follows
• Start
• Change gear
• Stop
• Accelerate
• Reverse
Simple Class Program
 Our first program contains a class and two objects of that class.
Although it’s simple, the program demonstrates the syntax and general features of classes in C++.

class smallobj //define a class int main() Output


{ { Data is 1066
private: smallobj s1, s2; //define two Data is 1776
int somedata; //class data objects of class smallobj
public: s1.setdata(1066); //call
void setdata(int d) //member function to set data member function to set
{ data
somedata = d; s2.setdata(1776);
} s1.showdata(); //call
void showdata() //member function to display data member function to display
{ data
cout << “Data is “ << somedata << endl; } s2.showdata();
}; return 0;
}
Classes
 A class is a description of a number of similar objects or
 A collection of objects with same properties and functions is known as class.
 A class is used to define the characteristics of the objects.
 It is used as a model for creating different objects of same type
 Each object of a class is known as an instance of its class

 Declaring a Class
 A class is declared in the same way as a structure is declared.
 The keyword class is used to declare a class.
 A class declaration specifies the variables and functions that are common to all objects of that
class.
 The variables declared in a class are known as member variables or data members.
 The functions declared in a class are called member functions
 Syntax
class identifier{
body of the class
};
 class it is a keyword to declare a class
 identifier it is the name of the class
 The class declaration always end with semi colon. All data members and member functions are
declared within the braces known as body of the class.
Access Specifiers
 The commands that are used to specify the access level of class members are known as access
specifiers. Two most important access specifiers are as follows:
 The private Access Specifier
• It is used to restrict the use of class member within the class.
• Any member of the class declared with private access specifier can only be accessed within the
class.
• The data members are normally declared with private access specifier. It is because the data of an
object is more sensitive.

 The public Access Specifier


• It is used to allow the user to access a class member within the class as well as outside the class.
• Any member of the class declared with public specifier can be accessed from anywhere in the
program.
• The member functions are normally declared with public access specifier. It is because the user
access functions of an object from outside the class
Creating Objects
 A class is simply a model or prototype for creating objects. It is like a new data type that contains
both data and functions
 An object is created in the same way as other variables are created
 When an object of a class is created, the space for all data members defined in the class is also
allocated in the memory according to their data types
 An object is also known as instance of the class. The process of creating an object of a class is also
called instantiation.
 Objects are sometimes called instance variables

 Syntax
 The syntax of creating an object of a class is also as follows:
class_name object_name;
 class_name it is the name of the class whose type of object is to be created
 object_name it is the name of the object to be created. The rules for object name are same as
the rules for declaring a variable.

 Example
 Test obj;
 The above example declares an object obj of type Test. The object contains all data members
that are defined in class Test.
Executing Member Functions
 An object of a particular class contains all data members as well as member functions defined
in that class
 The data members contains the values related to the object
 The member functions are used to manipulate data members
 The member functions can be executed only after creating an object
 The member functions can be executed only after creating an object

 Syntax
 object_name.function();
 object_name: It is the name of the object whose member function is to be executed
 function: It is the name of the member function to be executed. Any required
parameters are also passed to the member function in parenthesis.
 The object name and member function are separated by dot operator
 Example
 Test obj;
 obj. input();
Program Example
 We’ll use objects to represent distances measured in the English system
#include <iostream>
using namespace std;
class Distance //English Distance class int main()
{ {
private: Distance dist1, dist2; //define two lengths
int feet; dist1.setdist(11, 6.25); //set dist1
float inches; dist2.getdist(); //get dist2 from user
public: //display lengths
void setdist(int ft, float in) //set Distance to args cout << “\ndist1 = “; dist1.showdist();
{ cout << “\ndist2 = “; dist2.showdist();
feet = ft; inches = in; cout << endl;
} return 0;
void getdist() //get length from user }
{
Output
cout << “\nEnter feet: “; cin >> feet;
cout << “Enter inches: “; cin >> inches;
Enter feet: 10
}
Enter inches: 4.75
void showdist() //display distance
dist1 = 11’-6.25”
{
dist2 = 10’-4.75”
cout << feet << “\’-” << inches << ‘\”’;
} };
Example
 Write a class Marks with three data members to store three marks. Write three member functions
in() to input marks, sum() to calculate and return the sum and avg() to calculate and return the
average marks.
#include <iostream> int main(){
using namespace std; Marks m;
class Marks { int s; float a;
private: m.in();
int a, b, c; s= m.sum();
public: a= m.avg();
int in() { cout<<"Sum =" << s<<endl;
cout<<"Enter three marks:"; cout<<"Average="<<a;
cin>>a>>b>>c; }
return 0;
} Output
int sum() { Enter three marks: 50
return a+b+c; 40
} 50
float avg(){ Sum =140
return (a+b+c)/3.0; Average =46.666668
}
};

You might also like