UNIT 1
LECTURE 2LECTURE 2
Title : Object Oriented Programming
Subject Code : 3XT03
Semester : Third
Department : Electronics & Telecommunication Engineering
Prof. Avinash S. Kapse
Contents
Introduction to classes and object.
Declaration of class and objects.
Structure of C++ class.
Prof. Avinash S. Kapse
Last Lecture Review
In last lecture, we have seen following points:
Structure of
C++ program
Variable
s in C++
Input and Output
Statements
Prof. Avinash S. Kapse
Objectives
After completing this lecture, you will
come to know and understand the
following points:
After completing this lecture, you will
come to know and understand the
following points:
Classes and ObjectClasses and Object
Structure of
C++ class
Structure of
C++ class
Access Specifier
Scope Resolution
Operator
Prof. Avinash S. Kapse
Classes and Object
A class is an expanded concept of a data structure: instead
of holding only data, it can hold both data and functions.
An object is an instantiation of a class. In terms of
variables, a class would be the type, and an object would be the
variable.
Classes are generally declared using the keyword class,
with the format which is given on next slide:
Prof. Avinash S. Kapse
Cont…
class class_name
{
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
class_name:- This is a valid identifier for the class.
object_names:- This is an optional list of names for objects of this
class.
Prof. Avinash S. Kapse
Cont…
The body of the declaration can contain members, that can
be either data or function declarations, and optionally access
specifiers.
Access specifiers modify the access rights that the members
following them acquire:
• private
• protected
• public
Prof. Avinash S. Kapse
Cont…
private members of a class are accessible only from within
other members of the same class or from their friends.
protected members are accessible from members of their
same class and from their friends, but also from members of their
derived classes.
public members are accessible from anywhere where the
object is visible.
Prof. Avinash S. Kapse
Cont…
By default, all members of a class declared with the class
keyword have private access for all its members. Therefore, any
member that is declared before one other class specifier
automatically has private access. For example:
class CRectangle
{
int x, y;
public:
void set_values (int,int);
int area (void);
} rect;
Prof. Avinash S. Kapse
Cont…
Declares a class (i.e., a type) called CRectangle and an
object (i.e., a variable) of this class called rect. This class contains
four members: two data members of type int (member x and
member y) with private access (because private is the default
access level) and two member functions with public access:
set_values() and area(), of which for now we have only included
their declaration, not their definition.
Prof. Avinash S. Kapse
Objects
1. An object is an instance of class. An object is a thing like
vehicle, employee or anything.
2. An object consist of data and operations that manipulate those
data i.e. methods.
3. Syntax:
class_name object1, object2……..objectN;
where class_name is name of the class to which object belong and
object1, object2…..objectN are objects.
Prof. Avinash S. Kapse
Example
// classes example
#include <iostream.h>
class Crectangle
{
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
};
Header File
Class Declaration
Access Specifier
Function declaration
Prof. Avinash S. Kapse
Example 1
void Crectangle :: set_values (int a, int b)
{
x = a;
y = b;
}
main () {
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
}
Function definition
with two parameters a
and b
Object rect has been
created of class
CRectangle
Prof. Avinash S. Kapse
Explanation
The most important new thing in this code is the operator of
scope (::, two colons) included in the definition of set_values(). It is used
to define a member of a class from outside the class definition itself.
You may notice that the definition of the member function area()
has been included directly within the definition of the CRectangle class.
Note:- When we want to declare a function inside a class and definition
outside the class then use :: (Scope Resolution Operator).
Prof. Avinash S. Kapse
Cont…
1. Members x and y have private access.
2. By declaring them private we deny access to them from
anywhere outside the class.
3. CRectangle rect; This statement is used to create object rect of
class Crectangle.
4. rect.set_values (3,4); This statement is used to call a function
i.e. set_values(3,4). 3 is assigned to a and 4 is assigned to b
where a and b are parameters of function set_values().
Prof. Avinash S. Kapse
Example 2
#include <iostream.h>
class MyClass
{
int a,b;
void getData(int x, int y)
{
a=x;
b=y;
}
Function definition
with two parameters x
and y
Prof. Avinash S. Kapse
Cont…
void putData( )
{
cout<<“ a = ” <<a<<endl;
cout<<“ b = ” <<b<<endl;
}
};
void main () {
MyClass m1,m2;
m1.getData(5,10);
This function will
display the value of a
and b
Two objects have been
created i.e. m1 and m2
getData( ) function is
called with object m1.
Prof. Avinash S. Kapse
Cont…
m1.putData();
m2.getData(15,100);
m2.putData();
}
Output of the above program:
a = 5
b = 10
a = 15
b= 100
putData( ) function is
called with object m1.
getData( ) and
putData( ) function is
called with object m2.
Prof. Avinash S. Kapse
Example 3
WAP to accept and print student Roll Number and Name of a Student.
#include <iostream.h>
class MyStudent
{
int rollno;
char name[25];
void getData()
{
cout<<“Enter Roll Number and Name of a Student”<<endl;
cin>>rollno>>name;
}
Prof. Avinash S. Kapse
Cont…
void putData()
{
cout<<“ Roll Number : ” <<rollno<<endl;
cout<<“ Name of Student : ” <<name<<endl;
}
};
void main () {
MyStudent s1;
s1.getData();
s1.putData();
}
Prof. Avinash S. Kapse
Cont…
Output of the above program:
Enter Roll Number and Name of a Student
36 Bajirao Singham
Roll Number : 36
Name of Student: Bajirao Singham
Prof. Avinash S. Kapse
Exercise
1. WAP to read five numbers from user and calculate average.
2. WAP to calculate simple interest.
3. WAP to calculate percentage of a student by reading marks of
five subjects and maximum marks for each subject is 100.
Prof. Avinash S. Kapse
Home Work
1. What is object oriented programming?
2. What is polymorphism OOP?
3. What is Encapsulation OOP?
4. What is Inheritance OOP? Explain their types.
Prof. Avinash S. Kapse

Lecture 2

  • 1.
    UNIT 1 LECTURE 2LECTURE2 Title : Object Oriented Programming Subject Code : 3XT03 Semester : Third Department : Electronics & Telecommunication Engineering Prof. Avinash S. Kapse
  • 2.
    Contents Introduction to classesand object. Declaration of class and objects. Structure of C++ class. Prof. Avinash S. Kapse
  • 3.
    Last Lecture Review Inlast lecture, we have seen following points: Structure of C++ program Variable s in C++ Input and Output Statements Prof. Avinash S. Kapse
  • 4.
    Objectives After completing thislecture, you will come to know and understand the following points: After completing this lecture, you will come to know and understand the following points: Classes and ObjectClasses and Object Structure of C++ class Structure of C++ class Access Specifier Scope Resolution Operator Prof. Avinash S. Kapse
  • 5.
    Classes and Object Aclass is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Classes are generally declared using the keyword class, with the format which is given on next slide: Prof. Avinash S. Kapse
  • 6.
    Cont… class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } object_names; class_name:-This is a valid identifier for the class. object_names:- This is an optional list of names for objects of this class. Prof. Avinash S. Kapse
  • 7.
    Cont… The body ofthe declaration can contain members, that can be either data or function declarations, and optionally access specifiers. Access specifiers modify the access rights that the members following them acquire: • private • protected • public Prof. Avinash S. Kapse
  • 8.
    Cont… private members ofa class are accessible only from within other members of the same class or from their friends. protected members are accessible from members of their same class and from their friends, but also from members of their derived classes. public members are accessible from anywhere where the object is visible. Prof. Avinash S. Kapse
  • 9.
    Cont… By default, allmembers of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifier automatically has private access. For example: class CRectangle { int x, y; public: void set_values (int,int); int area (void); } rect; Prof. Avinash S. Kapse
  • 10.
    Cont… Declares a class(i.e., a type) called CRectangle and an object (i.e., a variable) of this class called rect. This class contains four members: two data members of type int (member x and member y) with private access (because private is the default access level) and two member functions with public access: set_values() and area(), of which for now we have only included their declaration, not their definition. Prof. Avinash S. Kapse
  • 11.
    Objects 1. An objectis an instance of class. An object is a thing like vehicle, employee or anything. 2. An object consist of data and operations that manipulate those data i.e. methods. 3. Syntax: class_name object1, object2……..objectN; where class_name is name of the class to which object belong and object1, object2…..objectN are objects. Prof. Avinash S. Kapse
  • 12.
    Example // classes example #include<iostream.h> class Crectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} }; Header File Class Declaration Access Specifier Function declaration Prof. Avinash S. Kapse
  • 13.
    Example 1 void Crectangle:: set_values (int a, int b) { x = a; y = b; } main () { CRectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); } Function definition with two parameters a and b Object rect has been created of class CRectangle Prof. Avinash S. Kapse
  • 14.
    Explanation The most importantnew thing in this code is the operator of scope (::, two colons) included in the definition of set_values(). It is used to define a member of a class from outside the class definition itself. You may notice that the definition of the member function area() has been included directly within the definition of the CRectangle class. Note:- When we want to declare a function inside a class and definition outside the class then use :: (Scope Resolution Operator). Prof. Avinash S. Kapse
  • 15.
    Cont… 1. Members xand y have private access. 2. By declaring them private we deny access to them from anywhere outside the class. 3. CRectangle rect; This statement is used to create object rect of class Crectangle. 4. rect.set_values (3,4); This statement is used to call a function i.e. set_values(3,4). 3 is assigned to a and 4 is assigned to b where a and b are parameters of function set_values(). Prof. Avinash S. Kapse
  • 16.
    Example 2 #include <iostream.h> classMyClass { int a,b; void getData(int x, int y) { a=x; b=y; } Function definition with two parameters x and y Prof. Avinash S. Kapse
  • 17.
    Cont… void putData( ) { cout<<“a = ” <<a<<endl; cout<<“ b = ” <<b<<endl; } }; void main () { MyClass m1,m2; m1.getData(5,10); This function will display the value of a and b Two objects have been created i.e. m1 and m2 getData( ) function is called with object m1. Prof. Avinash S. Kapse
  • 18.
    Cont… m1.putData(); m2.getData(15,100); m2.putData(); } Output of theabove program: a = 5 b = 10 a = 15 b= 100 putData( ) function is called with object m1. getData( ) and putData( ) function is called with object m2. Prof. Avinash S. Kapse
  • 19.
    Example 3 WAP toaccept and print student Roll Number and Name of a Student. #include <iostream.h> class MyStudent { int rollno; char name[25]; void getData() { cout<<“Enter Roll Number and Name of a Student”<<endl; cin>>rollno>>name; } Prof. Avinash S. Kapse
  • 20.
    Cont… void putData() { cout<<“ RollNumber : ” <<rollno<<endl; cout<<“ Name of Student : ” <<name<<endl; } }; void main () { MyStudent s1; s1.getData(); s1.putData(); } Prof. Avinash S. Kapse
  • 21.
    Cont… Output of theabove program: Enter Roll Number and Name of a Student 36 Bajirao Singham Roll Number : 36 Name of Student: Bajirao Singham Prof. Avinash S. Kapse
  • 22.
    Exercise 1. WAP toread five numbers from user and calculate average. 2. WAP to calculate simple interest. 3. WAP to calculate percentage of a student by reading marks of five subjects and maximum marks for each subject is 100. Prof. Avinash S. Kapse
  • 23.
    Home Work 1. Whatis object oriented programming? 2. What is polymorphism OOP? 3. What is Encapsulation OOP? 4. What is Inheritance OOP? Explain their types. Prof. Avinash S. Kapse