Object Oriented Programming /C++
Lecture 2
Structure in C and C++
Class
Object
Programming Example
1
Simple Example:
#include <iostream>
using namespace std;
void main()
{
cout << "Enter number 1: ";
int a;
cin >> a;
cout << "Enter number 2: ";
int b;
cin >> b;
Output
int s = a + b;
cout << "Sum = " << s;
}
Structures Revisited
Makes convenient to handle a group of logically related data items.
struct student //declaration
{
char name[20];
int roll_number;
float total_marks;
};
struct student A;// C declaration
student A; //C++ declaration
A.roll_number=999;
A.total_marks=595.5;
Final_Total=A.total_marks + 5;
C Structures limitation
Limitations in C
o No constructors and destructors.
o They do not permit data hiding.
o Structure does not support the inheritance.
Advantages in C++
o Can hold variables and functions as members.
o Can also declare some of its members as ‘private’.
o C++ introduces another user-defined type known as ‘class’ to incorporate all these
extensions.
Class: How to declare?
• A class definition starts with the keyword class followed by
the class name;
• and the class body, enclosed by a pair of curly braces.
• A class definition must be followed either by a semicolon or a
list of declarations.
Access
Specifiers
Class: Example
Functions and
variables declared
inside a class
declaration are said
to be members of
this class.
Object of class
Box
Properties of Class
Class members that have been declared as private
can be accessed only from within the class.
Public class members can be accessed from outside
the class also.
Supports data-hiding and data encapsulation features
of OOP.
Object
Object is the actual run time entity which holds
data and function that has been defined in the class.
Object declaration:
class_name obj1;
class_name obj2,obj3;
Class and Object
Accessing class members
• Object-name.function-name(actual-arguments);
• obj1.setdata(100,34.4);
Defining Member Functions
• Outside the class definition.
return-type class-name::function-name (argument
declaration)
{
Function body;
}
• Inside the class definition.
Same as normal function declaration.
Object as arrays
class employee
{
char name [30];
float age;
public:
void getdata(void);
void putdata(void);
};
employee manager[3];//array of manager
employee worker[7];//array of worker
Manager[i].putdata();
Access Specifiers: private and public
• Variables or functions declared after access specifier private
accessible only to member functions of the class.
• Variables or functions declared after access specifier public
accessible both by other members of the class and other part of
the program that contains the class.
• Protected - Data members and functions are available to derived
classes only.
• The default access for class members is private.
• Declaring data members with access specifier private is known
as data hiding. (encapsulation)
Public, Private
Private variable of class
Box.
Public variable of class
Box.
No problem, you can access a public variable through objects
of that class.
Problem, private variables can access only through member
functions of that class..
Class with member function
Definition:
Function that are declared to be part of a class are called member function of that
class.
ob ob2 Each object of a class
has its own copy of
Print_volu Print_volu
l w h me l w h me every variable
declared within the
class.
ob ob2
Print_volu Print_volu
l w h me l w h me
ob ob2
Print_volu Print_volu
l w h me l w h me
ob ob2
Print_volu Print_volu
l w h me l w h me
5
ob ob2
Print_volu Print_volu
l w h me l w h me
5 10
ob ob2
Print_volu Print_volu
l w h me l w h me
5 10 10
ob ob2
Print_volu Print_volu
l w h me l w h me
5 10 10
ob ob2
Print_volu Print_volu
l w h me l w h me
5 10 10
ob ob2
Print_volu Print_volu
l w h me l w h me
5 10 10
ob ob2
Print_volu Print_volu
l w h me l w h me
5 10 10
ob ob2
Print_volu Print_volu
l w h me l w h me
5 10 10
500
Member function with Scope
resolution operator ( :: )
1. Return type of the return_type class_name ::
function. function_name()
2. Class name {
3. Scope resolution //function body.
operator :: }
4. Function name with
parameter list.
5. Function body between
{}
Member function with Scope resolution
operator
Dealing with private data
It is common practice to declare all variables as private and
functions as public.
Example: Function with parameter