C++ Programming
Topics Covered=Inheritance, Manipulators
Example public access specifier with protected Visibility Mode
class A
{
protected:
int b;
public:
int a;
}
class B:protected A
{
cout<<a<<endl<<b; //a,b is accessible here
}
void main()
{
cout<<b ; //Not accessible here(because b becomes protected in Class B)
}
=================================================================
Array of object
An object of class represents a single record in memory, if we want more than one record of
class type, we have to create an array of class or object.
As we know, an array is a collection of similar type, therefore an array can be a collection of
class type.
The array of type class contains the objects of the class as its individual elements.
Thus, an array of a class type is also known as an array of objects.
1|Page 23 November 2020
C++ Programming
Syntax for Array of object
class class-name
{
datatype var1;
datatype var2;
----------
datatype varN;
method1();
method2();
----------
methodN();
};
class-name obj[ size ];
Eg:-
class pixel
{
int a,b;
void show(){}
}; a=12 a=12 a=12 a=12
Void main() b=30 b=30 b=30 b=30
{
pixel p[4]; 0 1 2 3
p[2].show();
}
=======================================================================
eg= ****************************Marksheet********************
2|Page 23 November 2020
C++ Programming
Cout<<”*********************Marksheet**********************”
Manipulators
● Manipulators are operators used in C++ for formatting output of the program.
● Manipulators are helping functions that can modify the input/output stream.
● It does not mean that we change the value of a variable, it only modifies the I/O stream using
insertion (<<) and extraction (>>) operators.
● Types of Manipulators-:
○ Non Parameterized
○ Parameterized
endl Manipulator:
● This manipulator has the same functionality as the ‘\n’ newline character.
For eg:- 1. cout << "Ice Cream" << endl;
cout << "Training";
Some Non Parameterized Manipulators are
○ hex: Read and write hexadecimal values for integers and it works same as the set-
base(16). //0 to 9, A to F
○ dec: Read and write decimal values for integers i.e. setbase(10). //0-9
○ oct: Read and write octal values for integers i.e. setbase(8). //0-7
Program 178
/*178.Example of Non Parameterized Manipulator with cout
eg endl, dec, oct, hex*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int a=100,b=64;
clrscr();
//endl used for line break
cout<<endl<<"This is endl Manipulator";
cout<<endl<<"Value of a is="<<a;
//dec manipulator
cout<<endl<<"Decimal of a number "<<(a)<<" is="<<dec<<a;
//oct manipulator
//used to change a number from dec base to octal base
cout<<endl<<"Octal of a number "<<a<<" is= "<<oct<<a;
cout<<endl<<a; //144
//hex manipulator
//used to change a number from decimal base(10) to hex base(16)
cout<<endl<<"Hexadecimal of a number "<<dec<<a<<" is="<<hex<<a;
3|Page 23 November 2020
C++ Programming
cout<<endl<<"Decimal of a number "<<(a)<<" is="<<dec<<a;
getch();
}
/*Output
This is endl Manipulator
Value of a is=100
Decimal of a number 100 is=100
Octal of a number 100 is=144 (100)10-(144)8
Hexadecimal of a number 100 is=64 (100)10-(64)16
Decimal of a number 64 is=100 (64)16-(?)10 */
4|Page 23 November 2020