Object Oriented Programming Using C++
Object Oriented Programming Using C++
4. Manipulators
• The scope resolution operator is used to reference the global variable or member function that is
out of the scope.
1) To access a global variable when there is a local variable with same name:
• Global variable:Global variables are the variables that are declared outside of
any function or class and can be accessed by any part of the program.
• local variable :Variables that are declared inside a function or block are local
variables. They can be used only by statements that are inside that function or
block of code. Local variables are not known to functions outside their own.
Example:
C++ program to show that we can access a global variable // using scope resolution operator ::
when there is a local // variable with same name
#include<iostream.h>
#include<conio.h>
cout<<“Here fun is called outside the class, which is the member function of the class Student”;
}
int main()
{
Student s1;
s1.fun();
return 0;
};
2. Memory Management Operator:
• In C++, two operators are used for the allocation and deallocation of
memory dynamically.
1. new operator
2. delete operator
1.New operator
• Syntax:
pointer variable=new datatype;
Example of new operator
// C++ program to demonstrate how to create dynamic variable // using new
# include<iostream.h>
#include<conio.h>
#include<memory.h>
int main()
{
// pointer to store the address returned by the new OUTPUT
int* ptr;
Address: 0x162bc20
// allocating memory for integer Value :10
ptr = new int;
// assigning value using dereference operator
*ptr = 10;
// printing value and address
cout << "Address: " << ptr << endl;
cout << "Value: " << *ptr;
return 0;
};
2.Delete operator:
• It is used to deallocate the memory which was used by new operator.
• Syntax:
delete ptr_variable;
Example:
int *a;
a=new int;
delete a;
Example of using new and delete operator
#include<iostream.h>
#include<conio.h>
#include<memory.h>
int main()
{
// pointer to store the address returned by the new
int* ptr;
// allocating memory for integer
ptr = new int;
// assigning value using dereference operator
*ptr = 10;
// printing value and address
cout << "Address: " << ptr << endl;
cout << "Value: " << *ptr;
//deallocate the memory
delete ptr;
return 0;
};
3.Manipulators:
• Manipulators are the operators that are used to format the data display.
• For Example:
cout<<“Hello”<<endl<<“Good morning”<<endl<<“bye”;
output:
Hello
Good morning
bye
2.setw() (field width operator):
• It specify common width for all the data and focus them to be printed in right justified
format only.
abcd
• For example: abc
cout<<setw(5)<<“abcd”<<endl; ab
a
cout<<setw(5)<<“abc”<<endl;
cout<<setw(5)<<“ab”<<endl;
cout<<setw(5)<<“a”<<endl;
Basic Input/ Output operators and functions in C++:
1. Cout (Output Operator) / [Insertion Operator ( <<) ]
Screen
• It insert the content of variable into the cout object and content of variable will be
appear on screen i.e. output devices.
• Example:
cout<<“Hello”;
2. Cin (Input Operator) [Extraction Operator (>>)]:
Keyboard
cin >> a
object Extraction Variable
operator
• It extracts the value from the keyboard and assigns it to the variable by using object cin.
• Example:
cin>>a;
Basic Input / Output functions in C++:
• In formatted console input output operations we uses following functions to make output in
perfect alignment.
• C++ provides many function to convert any file into perfect aligned format.
• These functions are available in header file<iomanip.h> . iomanip refers input output
manipulations.
Formatted IO Functions:
A) width(n) :
• This function is used to set width of the output.
• Syntax:
cout<<setw(int n);
• Example:
#include<iostream.h>
#include<iomanip.h>
int main()
{
int x=10;
cout<<setw(20)<<x;
return 0;
};
B) fill(char):
• This function is used to fill specified character at unused space.
• Syntax:
cout<<setfill('character')<<variable;
• Example:
#include<iostream.h>
#include<iomanip.h>
int main()
{
int x=10;
cout<<setw(20);
cout<<setfill(‘#’)<<x;
return 0;
};
Unformatted I/O Functions:
1. void get():
• It is a method of cin object used to input a single character from
keyboard.
• But its main property is that it allows wide spaces and newline character.
• Syntax:
char c=cin.get();
Example:
#include<iostream.h>
int main()
{
char c=cin.get();
cout<<c<<endl;
return 0;
};
2) void put():
• It is a method of cout object and it is used to print the specified character on the screen or
monitor.
• Syntax:
cout.put(variable / character);
Example:
#include<iostream.h>
int main()
{
char c=cin.get();
cout.put(c); //Here it prints the value of variable c;
cout.put('c'); //Here it prints the character 'c’;
return 0;
};
Structure of C++ Program:
Class declaration
Class Functions
Main Functions
1.Include header files:
• In these section programmer will include all the header files which are necessary for program execution .
<conio.h> : The word conio.h stands for console input-output and in programming, console
means output window which is the window where the result of the execution of our
program is displayed.
• In this section the programmer will declare the classes which are
necessary for a given program
• class class_name
{
access_specifier:
data_members;
function _members;
};
3. Class Functions declaration :
3. Inline Functions.
• Simple C++ Program :
Write a C++ program to calculate the square of a number
OUTPUT
#include<iostream.h>
#include<conio.h>
int main() Enter the number:7
{ Square of 7 is :49
clrscr();
int x,sqr;
cout<<“Enter the number:”;
cin>>x;
sqr=x*x;
cout<<“Square of ”<<x<<“ is:”<<sqr;
return 0;
};