0% found this document useful (0 votes)
89 views31 pages

C++ Object-Oriented Programming Guide

This document contains a chapter about classes and objects from a textbook on object oriented programming. It discusses the topics that will be covered in the chapter, including defining classes, creating objects, arrays of objects, static data members, and friend functions. It also includes sample assignment questions related to these topics for students to practice. The purpose is to introduce students to the basic concepts of classes in C++ and help them learn object oriented programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views31 pages

C++ Object-Oriented Programming Guide

This document contains a chapter about classes and objects from a textbook on object oriented programming. It discusses the topics that will be covered in the chapter, including defining classes, creating objects, arrays of objects, static data members, and friend functions. It also includes sample assignment questions related to these topics for students to practice. The purpose is to introduce students to the basic concepts of classes in C++ and help them learn object oriented programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name of staff:- Prof. Bhandare P.S.

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++.

 2.2 Class & Object: Introduction, specifying a class, access specifies,


defining member functions, creating Objects, memory allocations for
objects.

 2.3 Array of Objects, Object as function arguments.

 2.4 Static data members, static member function, friend Function

Assignment No 2.
 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)

 Explain syntax for declaring the function inside the class and outside the
class with example. (4 M)

 Define friend function. Write syntax of declaring it. (4M)

 Define structure. Give syntax of defining structure.(4M)

 Define a structure with it’s syntax. (2 M)

 Define class with it’s syntax. (2M)

 Write any two rules to define friend function. (2M)

 Write any two characteristics of static member function. (2 M)

 Differentiate between structure and class. (4 M)

 Explain object as function argument. (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)

 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)
2
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 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)

 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)

 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)

 Write a program to declare a class ‘Journal’ having data members


as journal-name, price & no-of-pages. Accept this data for two
objects & display the name of journal having greater price. (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 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)

3
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

About title of chapter :

 The title of chapter classes & objects gives idea about the new principal
defined in C++ similar to structure in C.

 In this chapter we will get information defining classes, creating objects.

 In this chapter students are going to learn new programming approach.

Central Idea of chapter

 Central Idea behind including this chapter in subject is that the student
will importance of the classes in OOP.

 Also Student will learn how programming can be organized in C++


language with the help of classes.

Importance of chapter :

 Studying this chapter is important is because this chapter introduce basic


things needed in OOP language.

 Also this chapter include structure of class.

 Also it will include different access specifiers.

Objectives of the chapter

 To study classes declaration.

 To study object creation.

 To study accessing member functions.

 To study static data members.

 To study memory allocation for object.

4
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Functions in C++ :

 functions play an important role in C program development.

 Dividing a program into functions is one of the major principles of top-


down, structured programming.

 Another advantage of using functions is that it is possible to reduce the


size of a program by calling and using them at different places in the
program.

 Recall that we have used a syntax similar to the following in developing


C programs.

◦ Void show() //Function Declaration

◦ Void main()

 {

 --------

 show(); //function call

 -----

 }

◦ Void show() //function definition

 {

 ----------

 ---------- // Function body

 When the function is called, control is transferred to the first statement in


the function body.

 The other statements in the function body are then executed and control
returns to the main program when the dosing brace is encountered.

 C++ is no exception. Functions continue to be the building blocks of C++


programs.
5
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 A function is a group of statements that together perform a task.

 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 −

◦ return_type function_name( parameter list )

◦ {

◦ body of the function

◦ }

◦ A function definition in C programming consists of a function


header and a function body. Here are all the parts of a function.

 Return Type − A function may return a value. The return_type is the


data type of the value the function returns.

 Some functions perform the desired operations without returning a value.


In this case, the return_type is the keyword void.

 Function Name: This is the actual name of the function. The function
name and the parameter list together constitute the function signature.

 Parameters − A parameter is like a placeholder. When a function is


invoked, you pass a value to the parameter.

 This value is referred to as actual parameter or argument. The parameter


list refers to the type, order, and number of the parameters of a function.

 Parameters are optional; that is, a function may contain no parameters.

 Function Body − The function body contains a collection of statements


that define what the function does.

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.

 The actual body of the function can be defined separately.

 A function declaration has the following parts

◦ return_type function_name( parameter list );

 For the above defined function max(), the function declaration is as


follows −

 int max(int num1, int num2);

 Parameter names are not important in function declaration only their type
is required, so the following is also a valid declaration

◦ int max(int, int);

7
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 Function declaration is required when you define a function in one source


file and you call that function in another file.

 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.

 When a program calls a function, the program control is transferred to the


called function.

 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.

 These variables are called the formal parameters of the function.

 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.

 In this case, changes made to the parameter inside the function


have no effect on the argument.

 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

 This means that changes made to the parameter affect the


argument.

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.

 A class is an extension of the idea of structure used in C.

 It is a new way of creating and implementing a user-defined data type.

C Structures
 We know that one of the unique features of the C language is structures.

 They provide a method for packing together data of different types.

 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

 consider the following declaration

10
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 struct student

 {

 char name [20] ;

 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.

 These fields are known as structure members or elements.

Specifying a Class
 A class is a way to bind the data and its associated functions together.

 It allows the data and functions to be hidden, if necessary, from external


use.

 When defining a class, we are creating a new abstract data type that can
be treated like any other built-in data type.

 Generally, a class specification has two parts:

◦ 1. Class declaration

◦ 2. Class function definitions

 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 class declaration is similar to a struct declaration.

 The keyword class specifies, that what follows is an abstract data of type
class_name.

 The body of a class is enclosed within braces and terminated by a


semicolon.

 The class body contains the declaration of variables and functions.

 These functions and variables are collectively called class members.

 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 getdata(int a, float b);

◦ void putdata(void);

◦ }; // ends with semicolon


13
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Notation to represent class

Creating Objects
 The declaration of an object is similar to that of a variable of any basic
type.

 The necessary memory space is allocated to an object at this stage.

 Objects can also be created when a class is defined by placing their


names immediately after the closing brace, as we do in the case of
structures.

 That is to say, the definition

 class Item

 {

 } x, y, z;

 would create the objects x, y and z of type item.

14
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Accessing Class Members


 As pointed out earlier, the private data of a class can be accessed only
through the member functions of that class.
 The main() cannot contain statements that access number and cost
directly.
 The following is the format for calling a member function:
 object-name . function-name(actual-arguments);
 For example, the function call statement x.getdata(100,75.5); is valid and
assigns the value 100 to number and 75.5 to coat of the object x by
implementing the getdata() function.
 Similarly, the statement x.putdata(); would display the values of data
members.
 Remember, a member function can be invoked only by using an object
(of the same class).
 It may be recalled that objects communicate by sending and receiving
messages.
 This is achieved through the member functions.
 For example, x.putdata(); sends a message to the object x
requesting it to display its contents.
 A variable declared as public can be accessed by the objects
directly.
 E.g

15
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Defining Member Functions


 Member functions can be defined in two places:

◦ Outside the class definition.

◦ Inside the class definition.

 It is obvious that, irrespective of the place of definition, the function


should perform the same task.

 Therefore, the code for the function body would be identical in both the
cases.

Outside the Class Definition


 An important difference between a member function and a normal
function is that a member function incorporates a membership 'identity
label' in the header.

 This 'label' tells the compiler which class the function belongs to.

 The general form of a member function definition is:

 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.

 The symbol :: is called the scope resolution operator.

16
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 For instance, consider the member functions getdat,a() and putdata() as


discussed above. They may be coded as follows:

 void item : : getdata(int a, float b)

 {

 number = a;

 Cost = b;

 }

 void item : : putdata(int a, float b)

 {

 cout<<“Number= ”<<number;

 cout<<“Cost= ”<<cost;

 }

Inside the Class Definition


 Another method of defining a member function is to replace the function
declaration by the actual function definition inside the class.

 For example, we could define the item class as follows:

◦ class item

◦ {

◦ int number;

◦ float cost;

◦ public:

◦ void getdata(int a, float b);

◦ void putdata(void)

◦ {

◦ cout « “number”;
17
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

◦ out « “cost”;

◦ }

◦ }

 When a function is defined inside a class, it is treated as an inline


function.

 Therefore, all the restrictions and limitations that apply to an inline


function are also applicable here.

 Normally, only small functions are defined inside the class definition.

Nesting of Member Functions


 Member function of a class can be called only by an object of that class
using a dot operator.

 However, there is an exception to this.

 A member function can be called by using its name inside another


member function of the same class.

 This is known as nesting of member functions.

Private Member Functions


 Although it is normal practice to place all the data items in a private
section and all the functions in public, some situations may require
certain functions to be hidden (like private data) from the outside calls.

 We can place functions in the private section.

 A private member function can only be called by another function that is


a member of its class.

 Even an object cannot invoke a private function using the dot operator.

 Consider a class as defined below:

 class sample

 {

 int m;
18
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 void read(void); // private member junction

 public:

 void update(void);

 void write(void);

 };

 If s1 is an object of sample, then

 s1 . read ( ) ; // won't work; objects cannot access private members


.

 However, the function read() can be called by the function update() to


update the value of m.

 void sample :: update(void)

 {

 read(); // simple call; no object used

 }

Memory Allocation for Objects


 Memory space for objects is allocated when they are declared and not
when the class is specified.

 This statement is only partly true.

 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.

 This is shown in Fig. below


19
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Static Data Members


 A data member of a class can be qualified as static.

 The properties of a static member variable are similar to that of a C static


variable.

 A static member variable has certain special characteristics.

◦ It is initialized to zero when the first object of its class is created.


No other initialization is permitted.

◦ 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.

 Static variables are normally used to maintain values common to the


entire class.

 For example, a static data member can be used as a counter that records
the occurrences of all the objects.

◦ Include <iostream>

◦ class item

◦ {

◦ static int count;

◦ int number;

◦ public:

◦ void getdata(int a)

◦ {

◦ number a; count ++;

◦ }

◦ void getcount(void)

◦ {

◦ cout « “count: “;

◦ cout « count «”\n”;

◦ }

◦ };

◦ int item :: 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);

◦ cout « "After reading data”;

◦ 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.

Static Member functions


 Like static member variable, we can also have static member functions.

 A member function that is declared static has the following properties:

◦ • A static function can have access to only other static members


(functions or variables) declared in the same class.

◦ • A static member function can be called using the-class name


(instead of its objects) :

 Syntax:- class-name :: function-name;

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);

◦ };

 The identifier employee is a user-defined data type and can be used to


create objects that relate to different categories of the employees.

◦ employee manager[3];

23
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

◦ employee foreman[15] ;

◦ employee worker[75];

WAP to accept & display data for 5 managers.

Objects as Function Arguments


 Like any other data type, an object may be used as a function argument.

 This can be done in two ways:

◦ A copy of the entire object is passed to the function.

◦ Only the address of the object is transferred to the function.

 The first method is called pass-by-value.

 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.

 The second method is called pass-by-reference.

 When an address of the object is passed, the called function works


directly on the actual object used in the call.

 This means that any changes made to the object inside the function will
reflect in the actual object.

 The pass-by reference method is more efficient since it requires to pass


only the address of the object and not the entire object.

◦ class time

◦ {

◦ int hours;

◦ int minutes;

◦ public:

◦ void gettime(int h, int m)

◦ {
24
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

◦ hours = h;

◦ minutes = m;

◦ }

◦ void puttime(void)

◦ {

◦ cout << hours « " hours and “;

◦ cout « minutes « " minutes “ « "\n';

◦ }

◦ void sum(time, time); // declaration with objects os arguments

◦ };

◦ void time :: sum(time tl, time t2)

◦ {

◦ minutes = tl.minutes + t2.minutes;

◦ hours = minutes/60;

◦ minutes = minutes%60;

◦ Hours = hours + tl.hours + t2.hours;

◦ }

◦ void main()

◦ {

◦ time T1, T2, T3;

◦ T1.gettime(2,46);

◦ T2.gettime(3,30);

◦ T3.sum(T1, T2);

◦ cout « "T1 = “<< Tl.puttime();


25
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

◦ cout « "T2 = “<< T2.puttime();

◦ cout « "T3 =“<< T3.puttime();

◦ }

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.

 However, there could be a situation where we would like two classes to


share a particular function.

 For example, consider a case where two classes, manager and scientist,
have been defined.

 We would like to use a function income_tax() to operate on the objects of


both these classes.

 In such situations, C++ allows the common function to be made friendly


with both the classes, thereby allowing the function to have access to the
private data of these classes.

 Such a function need not be a member of any of these classes.

 To make an outside function "friendly" to a class, we have to simply


declare this function as a friend of the class as shown below:

26
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 The function declaration should be preceded by the keyword friend.


 The function is defined elsewhere in the program like a normal C++
function.
 The function definition does not use either the keyword friend or the
scope operator :: .
 The functions that are declared with the keyword friend are known as
friend functions.
 A friend function, although not a member function, has full access rights
to the private members of the class.
A friend function possesses certain special characteristics:

 It is not in the scope of the class to which it has been declared as


friend.
 Since it is not in the scope of the class, it cannot be called using the
object of that class.
 It can be invoked like a normal function without the help of any
object.
 Unlike member functions, it cannot access the member names
directly and has to use an object name and dot membership
operator with each member name. (e.g. A.x).
 It can be declared either in the public or the private part of a class
without affecting its meaning.
 Usually, it has the objects as arguments.

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;
 }

Differentiate between class & structure

Structure Class

Structure is collection of variable of Class is collection of variables of different data


different data types types & functions

“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:

structStruct_name class class_name

{ {

variable 1; private:

variable 2; member variable declaration;

………….. member function declaration;

variable n; public:

}struct_var; member variable declaration;

member function declaration; };

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)

 Define class with it’s syntax. (2M)

 Write any two rules to define friend function. (2M)

 Write any two characteristics of static member function. (2 M)

 Explain how memory is allocated to an object of a class with diagram. (4 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)

 Explain object as function argument. (4 M)

 How many ways we can define member function in class ? Give it’s syntax. (4 M)

 Differentiate between structure and class. (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)

 Explain memory allocation for objects. (4M)

 Write a program to declare a class ‘Journal’ having data members as journal-


name, price & no-of-pages. Accept this data for two objects & display the
name of journal having greater price. (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 define a structure ‘Tender’ having data members tender-no.,


cost and company-name. Accept & display this data for two variable of this
structure.(8mks)

 Explain the concept of friend function. (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)

You might also like