Oop Lec1
Oop Lec1
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
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++.
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.
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
}
};