C++ Object-Oriented Programming Guide
C++ Object-Oriented Programming Guide
Chapter 2
classes & objects
-by-
Prof. Bhandare P. S.
SVERI’s COE(Poly), Pandharpur
1
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Syllabus:
2.1 Structures in C++.
Assignment No 2.
How memory is allocated to the objects of class, explain with diagram.(
2M)
Explain syntax for declaring the function inside the class and outside the
class with example. (4 M)
Write a program to declare class ‘staff’ having data members as name and
post. Accept this data for 5 staffs and display name of staff who are HOD.
(8 mks)
3
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
The title of chapter classes & objects gives idea about the new principal
defined in C++ similar to structure in C.
Central Idea behind including this chapter in subject is that the student
will importance of the classes in OOP.
Importance of chapter :
4
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Functions in C++ :
◦ Void main()
{
--------
-----
}
{
----------
The other statements in the function body are then executed and control
returns to the main program when the dosing brace is encountered.
Every C program has at least one function, which is main(), and all the
most trivial programs can define additional functions.
Defining a Function
The general form of a function definition in C programming language is
as follows −
◦ {
◦ }
Function Name: This is the actual name of the function. The function
name and the parameter list together constitute the function signature.
6
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
E.g.
Function Declarations
A function declaration tells the compiler about a function name and how
to call the function.
Parameter names are not important in function declaration only their type
is required, so the following is also a valid declaration
7
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
In such case, you should declare the function at the top of the file calling
the function.
Calling a Function
While creating a C function, you give a definition of what the function
has to do.
To use a function, you will have to call that function to perform the
defined task.
A called function performs a defined task and when its return statement is
executed or when its function-ending closing brace is reached, it returns
the program control back to the main program.
To call a function, you simply need to pass the required parameters along
with the function name, and if the function returns a value, then you can
store the returned value.
8
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Function Arguments
If a function is to use arguments, it must declare variables that accept the
values of the arguments.
Formal parameters behave like other local variables inside the function
and are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be
passed to a function −
9
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Call by value:-
This method copies the actual value of an argument into the
formal parameter of the function.
Call by reference:
This method copies the address of an argument into the formal
parameter. Inside the function, the address is used to access the
actual argument used in the call. s
Introduction to class
The most important feature of C++ is the "class".
Its significance is highlighted by the fact that Stroustrup initially gave the
name "C with classes" to his new language.
C Structures
We know that one of the unique features of the C language is structures.
It is a user-defined data type with a template that serves to define its data
properties.
Once the structure type has been defined, we can create variables of that
type using declarations that are similar to the built-in type declarations
10
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
struct student
{
int roll_number;
float total_marks;
};
The keyword struct declares student as a new data type that can
hold three fields of different data types.
Specifying a Class
A class is a way to bind the data and its associated functions together.
When defining a class, we are creating a new abstract data type that can
be treated like any other built-in data type.
◦ 1. Class declaration
The class declaration describes the type and scope of its members.
The class function definitions describe how the class functions are
implemented.
11
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
The keyword class specifies, that what follows is an abstract data of type
class_name.
They are usually grouped under two sections, namely, private and public
to denote which of the members are private and which of them are public.
The keywords private and public are known as visibility labels. These
keywords are followed by a colon.
The class members that have been declared as private can be accessed
only from within the class.
On the other hand, public members can be accessed from outside the
class also.
The data hiding (using private declaration) is the key feature object-
oriented programming.
12
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
The variables declared inside the class are known as data members and
the functions are known as member functions.
Only the member functions can have access to the private data members
and private functions.
However, the public members (both functions and data) can be accessed
from outside the class
◦ class item
◦ {
◦ int number;
◦ float cost;
◦ public:
◦ void putdata(void);
Creating Objects
The declaration of an object is similar to that of a variable of any basic
type.
class Item
{
} x, y, z;
14
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
15
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Therefore, the code for the function body would be identical in both the
cases.
This 'label' tells the compiler which class the function belongs to.
The membership label class-name :: tells the compiler that the function
function-name belongs to the class class-name.
That is, the scope of the function is restricted to the clan-name specified
in the header line.
16
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
{
number = a;
Cost = b;
}
{
cout<<“Number= ”<<number;
cout<<“Cost= ”<<cost;
}
◦ class item
◦ {
◦ int number;
◦ float cost;
◦ public:
◦ void putdata(void)
◦ {
◦ cout « “number”;
17
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
◦ out « “cost”;
◦ }
◦ }
Normally, only small functions are defined inside the class definition.
Even an object cannot invoke a private function using the dot operator.
class sample
{
int m;
18
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
public:
void update(void);
void write(void);
};
{
}
Actually, the member functions are created and placed in the memory
space only once when they are defined as a part of a class specification.
Since all the objects belonging to that class use the same member
functions, no separate space is allocated for member functions when the
objects are created.
Only space for member variables is allocated separately for each object.
Separate memory locations for the objects are essential, because the
member variables will hold different data values for different objects.
◦ Only one copy of that member is created for the entire class and is
shared by all the objects of that class, no matter how many objects
are created.
◦ It is visible only within the class, but its lifetime is the entire
program.
20
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
For example, a static data member can be used as a counter that records
the occurrences of all the objects.
◦ Include <iostream>
◦ class item
◦ {
◦ int number;
◦ public:
◦ void getdata(int a)
◦ {
◦ }
◦ void getcount(void)
◦ {
◦ cout « “count: “;
◦ }
◦ };
◦ void main()
◦ {
◦ item a. b, c;
21
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
◦ a.getcount();
◦ b.getcount();
◦ c.getcount();
◦ a.getdata(100);
◦ b.getdata(200);
◦ c.getdata(300);
◦ a.getcount();
◦ b.getcount();
◦ c.getcount();
◦ }
Note that the type and scope of each static member variable must be
defined outside the class definition.
This is necessary because the static data members are stored separately
rather than as a part of an object.
Since they are associated with the class itself rather than with any class
object, they are also known as class variables.
22
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Arrays of Objects
We know that an array can be of any data type including strut.
Similarly, we can also have arrays of variables that are of the type class.
Such variables are called arrays of objects. Consider the following class
definition:
◦ class employee
◦ {
◦ char name[30];
◦ float age;
◦ public:
◦ void getdata();
◦ void putdata(void);
◦ };
◦ employee manager[3];
23
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
◦ employee foreman[15] ;
◦ employee worker[75];
Since a copy of the object is passed to the function, any changes made to
the object inside the function do not affect the object used to call the
function.
This means that any changes made to the object inside the function will
reflect in the actual object.
◦ class time
◦ {
◦ int hours;
◦ int minutes;
◦ public:
◦ {
24
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
◦ hours = h;
◦ minutes = m;
◦ }
◦ void puttime(void)
◦ {
◦ }
◦ };
◦ {
◦ hours = minutes/60;
◦ minutes = minutes%60;
◦ }
◦ void main()
◦ {
◦ T1.gettime(2,46);
◦ T2.gettime(3,30);
◦ T3.sum(T1, T2);
◦ }
Friendly Function
Private members cannot be accessed from outside the class.
That is, a non-member function cannot have an access to the private data
of a class.
For example, consider a case where two classes, manager and scientist,
have been defined.
26
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Returning object
As we can pass object as function arguments, objects can be returned
from function.
◦ E.g.
◦ class data
◦ {
◦ int a,b;
◦ public:
27
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
◦ void get();
◦ friend data sum (data, data);
◦ void show();
◦ };
data sum(data a1,data a2)
{
data a3;
a3.a=a1.a+a2.a;
a3.b=a1.b+a2.b;
return a3;
}
void main() {
clrscr();
data a,b,c;
a.get();
b.get();
c=sum(a,b);
c.show();
}
void data::get()
◦ { cout<<"PLEASE ENTER FOR A:->";
◦ cin>>a;
◦ cout<<"PLEASE ENTER FOR B:->";
◦ cin>>b;
}
◦ void data::show()
28
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
◦ { cout<<"A= "<<a<<endl;
◦ cout<<"B= "<<b<<endl;
}
Structure Class
“struct” keyword is used while declaring “class” keyword is used while declaring a
structure class
Structure variables can be created Class variables can be created which are called
as objects of class
Access specifiers are not available Access specifiers like private, public, protected
are available
Data hiding is not achieved Data hiding is achieved with private keyword
Syntax: Syntax:
{ {
variable 1; private:
variable n; public:
29
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Winter-15
How memory is allocated to the objects of class, explain with diagram.( 2M)
State any four characteristics of static data members.(2M)
Define friend function. Write syntax of declaring it.(4M)
How function is defined outside of class, write general syntax and example of same.
(4M)
Define friend function. Write syntax of declaring it. (4M)
Define structure. Give syntax of defining structure.(4M)
Write a program to declare class ‘staff’ having data members as name and post.
Accept this data for 5 staffs and display name of staff who are HOD. (8 mks)
Summer-15
Write a program to create a class “student” having data member as name, roll no. and
percentage to read and display details for 10 students. (4 M)
Explain : (4 M)
(i) Static member function
(ii) Friend function
Explain syntax for declaring the function inside the class and outside the class with
example. (4 M)
Write a program to show use of passing object as a parameter to function showdata
() for a class “student” having data member as name and roll no. & member function
as getdata (). (4 M)
Explain memory allocation for object with example. (4 M)
Write a program to create a class “employee” with data member as name, designation
and basic salary & gross salary. Create member functions as getdata () to read and
showdata () to display details. Create sum () as friend function to calculate gross
salary. gs = bs + 0.5 ∗ bs + 9.0 ∗ bs ; (8 M)
Winter-14
Define a structure with it’s syntax. (2 M)
Write a program to define a class student having data members name and roll no.
Accept and display data for one object. (4 M)
30
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Write a program to declare a class staff having data members as name and post.
Accept and display data for five staff members. (Using array of object). (4 M)
How many ways we can define member function in class ? Give it’s syntax. (4 M)
Summer-14
Write a program to define a structure ‘Tender’ having data members tender-no.,
cost and company-name. Accept & display this data for two variable of this
structure. (4M)
Write a program to define a class having data members principle, duration &
rate-of-interest. Declare rate-of-interest as static member variable. Calculate the
simple interest & display it for one object. (4M)
Write a program to declare a class ‘staff’ having data members as name &
department. Accept this data for 10 staffs & display names of staff that are in
cm department. (8M)
Winter-13
Write a program to calculate weight of objects of different planets using
formula weight = m × g.
Where m = mass of object
g = gravitational force and declare g as static member variable. (8M)
How memory allocation is done when multiple objects are created ? Explain. (4M)
Write a program to declare a class ‘staff’ having data members as name and post.
Accept this data for 5 staffs and display names of staff who are H.O.D. (8M)
Differentiate between call by value and call by reference methods.(4M)
31
Subject: Object oriented programming (17432)