Object Oriented Programming: Mangesh P. Joshi
Object Oriented Programming: Mangesh P. Joshi
Black board
Students
Mangesh P. Joshi
Assistant professor,
Department of Industrial Engineering
[email protected],
Mob: 8087946883
Introduction
A class is a collection of Similar type of entities
called as objects. Eg ( Students, teacher, second
year, Furniture)
Second Year Class Teacher
An object is a basic runtime entities which may
represent a person or a thing.
Students
Features of OOPs
1. Emphasis is on data rather than procedure.
2. Programs are divided into what are known as objects.
3. Data structures are designed such that they characterize the object.
4. Functions that operate on the data of an object are tied together in the data structure.
5. Data is hidden & cannot be accessed by external functions.
6. Objects may communicate with each other through functions.
7. New data and functions can be easily added whenever necessary.
8. Follows bottom up approach in program design.
Data Types:
1. Integer
represented as int. Various examples of integer data variables –
Roll number, Age, counter – 21, 36, 1 (numbers without decimal point)
2. Float
represented as float. Various examples of float data variables
marks, height, salary (2.5, 5.5,7.8 lakh- numbers with decimal points)
3 Character
represented as char. Various examples of char data variables
name, address, enrollment number
Some important terminology
Variable declaration: stating the type of variable
Variable declaration and definition: stating the type as well as value of variable
} main() Ends
// Remember C++ is a Case Sensitive programming Language and everything inbuilt is in lower case
#include<iostream.h>
#include<conio.h>
class Second
{
};
class Third
{
};
void main()
{
Second s1;
Third s2;
}
What header file includes
#include<iostream.h>
#include<conio.h>
It includes inbuilt functions such as clrscr() function which is used to clear whatever is
displayed on the monitor screen and getch() function which is used to get a character
from user to end the program.
public and private data/variables
private section of class
#include<iostream.h>
#include<conio.h> Variable declared as private
can be indirectly accessed
class Second from public section of the
{ class.
private: private variables can not be
char password[10]; accessed from main function.
float salary;
}
How to display data in a basic C++ program
#include<iostream.h>
#include<conio.h>
class student
{
private: // Here is no need to write private. If not written it is assumed by default as private
int Age;
public:
void showdata()
{
cout<<“I am the showdata() function declared and defined in public section”; // this will display “ ”
cout<<Age;
cout<<“ Roll = “<<roll<<endl<<“Age = ”<<age; Student
}
}; void main()
{
student s1; //classname objectname
s.showdata(); //any public variable / function can be accessed from here using object name, dot operator
}
How to get data and display in a basic C++ program
#include<iostream.h>
#include<conio.h>
class student
{
private: // Here is no need to write private. If not written it is assumed by default as private
int age, height;
public:
void getdata() void main()
{ {
cout<<“Enter age and height of student”; student s1, s2;
cin>>age>>height; s1.getdata();
} s2.showdata();
void showdata() }
{
Student
cout<<age;
cout<<“\n ”;
cout<<height;
}
};
How to get data and display in a basic C++ program – with description
#include<iostream.h>
#include<conio.h>
class student
{
int age, year; // private variables age and year, only accessed within the class and not from main()
public:
void getdata() //getdata() function declaration and definition. it can be accessed anywhere from the program
{
cout<<"Enter age and year";
cin>>age;
cin>>year;
}
void showdata() //showdata() function declaration and definition. it can be accessed anywhere from the program
{
cout<<"\n\n"<<"Age ="<<age<<" Year= "<<year;
}
};
void main()
{
clrscr(); // clear the previously written content from the screen
student s; //user defined data type. Object S is created of class student
s.getdata(); //direct access to getdata()
s.showdata(); //direct access to showdata()
getch(); // program will not terminate until user presses any key
}
#include<iostream.h>
#include<conio.h>
class student
{
int age, year; // private variables age and year, only accessed within the class and not from main()
void getdata() // private function getdata() only accessed within the class and not from main()
{
cout<<"Enter age and year";
cin>>age; // indirect access
cin>>year;
}
public:
void showdata()
{
getdata(); //calling of getdata() function
cout<<"\n\n"<<"Age ="<<age<<" Year= "<<year;
}
};
void main()
{
clrscr();
student s; //user defined data type. Object S is created
// s.getdata(); //direct access to getdata() is not allowed since it is private function
s.showdata(); //direct access to showdata() and there by indirectly accessing getdata() thorugh showdata()
getch();
}
Ways of Defining body of functions – function definition outside class
#include<iostream.h>
#include<conio.h>
void student :: showdata()
class student {
{ cout<<“Age= ”<<age;
private: cout<<“\n ”;
int age, height; cout<<“Height = ”<<height;
public: }
void getdata(); // function declaration
void showdata(); // function declaration void main()
}; {
student s1;
void student :: getdata() s1.getdata();
{ s1.showdata();
cout<<“Enter age and height of student”; }
cin>>age>>height;
}
Classifying data as public or private and assigning values
#include<iostream.h>
#include<conio.h>
class employee
{
private:
float salary; // it is declared as private as this is private information of an employee
public:
int account_number; //it is declared as public as this information must be share with people for money transfer
void showdata()
{
cout<<“Account Number= “<<account_number;
cout<<“Salary = ”<<salary;
}
};
void main()
{
employee e1; // e1 object of employee class is created
e1.account_number = 19310110005897; // this will work since account_number is public. You can assign value here as well.
e1.salary = 55000; // this will not work since salary is private. You can not assign value here.
e1.showdata();
}
Entering data from main() function and passing it to a class
#include<iostream.h>
#include<conio.h>
class employee
{
private:
float salary; // it is declared as private as this is private information of an employee
public:
int account_number; //it is declared as public as this information must be share with people for money transfer
void getdata(float sal, int acno)
{
salary = sal;
account_number = acno;
}
void showdata()
{ void main()
cout<<“Account Number= “<<account_number; {
cout<<“Salary = ”<<salary; employee e1;
} e1.getdata(55000,19310110005897)
}; e1.showdata();
}
Condition evaluation function – If-else statement OR If-elseif-else statement
if(num1<num2)
{
cout<<“Largest number is num2”;
}
elseif(num1>num2)
{
cout<<“Largest number is num1”;
}
else
{
cout<<“Both the numbers are equal”
}
Control loop structure – for loop
class Second
{
private:
char password[10];
float salary;
public:
int account_number;
call
void getdata();
void showdata(); sending no
}; value back to
void main() main()
{ function
}
What is the meaning of return type?- int
#include<iostream.h>
#include<conio.h>
class Second
{
private:
char password[10];
float salary;
public:
int account_number;
call
int getdata();
void showdata(); sending int
}; type data back
void main() to main()
{ function
}
What is the meaning of return type?- float
#include<iostream.h>
#include<conio.h>
class Second
{
private:
char password[10];
float salary;
public:
int account_number;
call
int getdata();
void showdata(); sending float
}; type data back
void main() to main()
{ function
}
#include<iostream.h>
#include<conio.h>
WAP to find out largest number of two user entered number
class number
{
private:
int num1, num2; void main()
public: {
void getdata() clrscr();
{ number n;
cout<<“Enter two numbers”; n.getdata();
cin>>num1>>num2; n.findlargest();
} getch();
void findlargest() }
{
if(num1<num2)
{
cout<<“Largest number is ”<<num2;
}
else
{
cout<<“Largest number is ”<<num1;
}
}
};