Professional Documents
Culture Documents
)
A Focused Approach
Before taking printout of this chapter, please ensure that this material has not been covered
in the printed course material provided to you (by us).
class class_name
access_specifier:
data functions
access_specifier:
data functions
} object_list;
The object_list is optional. The object_list is used to declare objects of the class. The
class_name is the name of the class. The access_specifier can either public, private or
protected. The members of the class by default are private to the class. If the access_specifier
is private then members of the class are not accessible outside the class. If the
access_specifier is public then members of the class can be accessed from outside the class.
The protected access_specifier is needed at the time of inheritance. The members can be
accessed using an objects name, a dot operator and name of the member. Here is a program
which shows how classes and objects are created.
#include<iostream>
class cube
public:
double side;
double volume()
return(side*side*side);
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 2
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
}
};
int main()
double volume1=0;
cube c1,c2;
cout << "The volume of the cube is : " << c1.volume() << endl;
c2.side=c1.side +2;
cout << "The volume of the second cube is : " << c2.volume() << endl;
return(0);
The program consists of a class cube which has data member side of type double and member
function which calculates the volume of the cube. The statement
class cube
public:
double side;
double volume()
return(side*side*side);
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 3
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
declare that access_specifier is public for data member side and member function volume.
These members can be accessed from the other parts of the program. The statement
cube c1,c2;
access the data member of the cube. The member is accessed by specifying the name of the
object as c1 then dot operator and then name of the variable side. The length entered by the
user is stored in c1.side. In the statement
cout << "The volume of the cube is : " << c1.volume() << endl;
c1.volume() calls the member function volume which returns the volume of the cube of side
whose length is entered by the user. The statement
c2.side=c1.side +2;
equates the side of object c2 to side of object c1 increased by 2. The objects c2 and c1 are
different. The statement
cout << "The volume of the second cube is : " << c2.volume() << endl;
};
The derived_name is the name of the derived class. The base_name is the name of the base
class. The access_specifier can be private, public or protected. If the access_specifier is
public then all public members of the base class become public members of the derived class
and protected members of the base class become the protected members of the derived class.
If the access_specifier is private then all public and protected members of the base class will
become private members of the derived class. If the access_specifier is protected then the
public and protected members of the base class become the protected members of the derived
class. Whether access_specifier is public, private or protected, private members of the base
class will not be accessed by the members of the derived class.
The access_specifier protected provides more flexibility in terms of inheritance. The private
members of the base class cannot be accessed by the members of the derived class. The
protected members of the base class remain private to their class but can be accessed and
inherited by the derived class. The protected members of the base class will remain private to
the other elements of the program.
A derived class can inherit one or more base classes. A constructor of the base is executed
first and then the constructor of derived class is executed. A destructor of derived class is
called before the destructor of base class. The arguments to the base class constructor can be
passed as follows:-
derived_constructor (argument list): base1 (arg_list)
base2(arg_list1)
baseN(arg_list)
The derived_constructor is the name of the derived class. The argument list is list of the data
members of the derived class. The base1 is name of the base class. The arg_list is the list of
the members of the base class. Here is a program which illustrates the features of inheritance.
#include<iostream>
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 5
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
class shape
private :
double length;
protected:
double breadth;
public :
double len()
return(length);
length=length1;
breadth=breadth1;
//shape() { }
};
class shape1
public:
double height;
shape1(double height1)
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 6
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
height=height1;
//shape1() { }
};
public:
double volume()
return(height*breadth*len());
double bre()
return(breadth);
double ht()
return(height);
};
int main()
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 7
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
cuboid c1(2.4,3.5,6.7);
cout << "The length of the cuboid is : " << c1.len() << endl;
cout << "The breadth of the cuboid is : " << c1.bre() << endl;
cout << "The height of the cuboid is : " << c1.ht() << endl;
cout << "The volume of the cuboid is : " << c1.volume() << endl;
return(0);
The program has two base classes shape and shape1 and one derived class called cuboid
which inherits shape as public and shape1 as private. The public and protected members of
shape become pubic and protected members of derived class cuboid. The private members of
shape remain private to the class shape. The members of shape1 class become the private
members of the derived class cuboid.
The statement
states that class cuboid inherits class shape as public and class shape1 as private. The
statement
declares the constructor of the class cuboid. When constructor of class cuboid is called first
constructor of shape is executed and then constructor of shape1 is executed and after that the
constructor of cuboid is executed. The statements
double volume()
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 8
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
{
return(height*breadth*len());
calcula te the volume of the cuboid. The class cuboid cannot access the private data member
length of the shape class. It access the length by calling the function len() which returns the
private data member length. The data member breadth becomes the protected member of the
class cuboid. The height which is public member of shape1 class becomes the private
member of the class cuboid as it inherits the shape 1 class as private. The statements
double bre()
return(breadth);
returns the breadth of the cuboid as data member breadth cannot be accessed outside the
class as it is protected member of cuboid. The statement
double ht()
return(height);
returns the height of the cuboid as data member height cannot be accessed outside the class
as height is the private data member of the class cuboid. The statement
cuboid c1(2.4,3.5,6.7);
creates an object c1 of type cuboid. The constructor is called to initialize the values of the
cuboid. The constructor of shape is executed and then constructor of shape1 is executed and
then finally constructor of cuboid is executed. The statement
cout << "The length of the cuboid is : " << c1.len() << endl;
displays the length of the cuboid as c1.len() calls the len() function of class shape which is
also the public member function of cuboid. The state ment
cout << "The breadth of the cuboid is : " << c1.bre() << endl;
displays the breadth of the cuboid. As the data member breadth cannot be accessed directly
as it is protected member of the class cuboid so the function bre() returns the breadth of the
cuboid. The statement
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 9
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
cout << "The height of the cuboid is : " << c1.ht() << endl;
displays the height of the cuboid. The data member height cannot be accessed directly as it is
private member of class cuboid so it is accessed through the function ht() which returns
height.
#include<iostream>
class employee
public:
int week;
int year;
return(salary*week);
return(salary*week*year);
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 10
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
}
employee(int week1)
week=week1;
week=week1;
year=year1;
};
int main()
int sal;
double sal2;
employee emp1(10);
employee emp2(10,3);
cout << "Enter the no years for first employee" << endl;
cout << endl << "Enter the salary per week for first employee" << endl;
cout << "The total salary of first employee is : " << emp1.calculate(sal) << endl;
cout << endl << "Enter the salary per week for second employee is : " << endl;
cout << "The total salary of second employee is for one year: " <<
emp2.calculate(sal2) << endl;
return(0);
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 11
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
}
Objective Questions
Set 2
1. Which of the following is not a valid C++ identifier?
(a) _temp
(b) break
(c) BREAK
(d) _brk
2. Which one of the following is correct?
(a) The formal and actual parameter names in function should have different names
(b) The local and global variables can have same name
(c) The constants of an uninitialised character array cells will contain zeros
(d) The contents of external variables are stored in interval registers of CPU
3. The output of the following program segment will be
int main ()
{
increment ();
}
void increment ()
{static x = 5;
cout<<x= <<x;
x = x + 5;
}
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 12
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
(a) 5 10
(b) 6 10
(c) 10 5
(d) 0 5
4. A program segment defines
int I = 8, j = 5;
what will be the value of expression (I > 0) && (j<5)?
(a) 8
(b) 5
(c) 0
(d) 1
5. Which of the numerical values is not a valid constant?
(a) 0xAB12
(b) 199
(c) 07891
(d) 0321
6. Which of the following converts a high level program to machine executable code?
(a) translator
(b) assembler
(c) interpreter
(d) compiler
7. A 32 bit address bus can address upto
(a) 4 megabytes
(b) 4 terabytes
(c) 4 gegabytes
(d) 32 gigabytes
8. Which of the following is an accounting software package?
(a) focus
(b) windows 2000
(c) foxpro
(d) tally
9. In relational database, a relation is represented by
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 13
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
(a) field
(b) record
(c) table
(d) primary key
10. Which of the following is a volatile memory?
(a) hard disk
(b) floppy disk
(c) ROM
(d) RAM
Answers: 1. b 2. d 3. a 4. c 5. a 6. d
7. c 8. d 9. b 10. d
TRUE/FALSE
11. Pentium is a 32 bit machine because it has got a 32 bit address bus.
12. Unix is a multiuser operating system.
13. C is a procedural programming language.
14. A C++ compiler actually translates a C++ source code into the equivalent machine
code of the target CPU.
15. Operating system acts as a resource manager for any computer system.
16. When one needs to connect all the computers in one building, then it is preferable to
use a LAN.
17. TCP/IP is actually two protocols rolled into one.
18. If is a valid C++ identifier.
19. A-23 is a valid C++ constant.
20. Flash memory is a writable non volatile memory.
Answer: 11. F 12. T 13. T 14. T 15. 15 16.
T
17. T 18. T 19. F 20. T
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 14