0% found this document useful (0 votes)
21 views

Object Oriented Programming Using C++

Presentation

Uploaded by

antarasartale11
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)
21 views

Object Oriented Programming Using C++

Presentation

Uploaded by

antarasartale11
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
You are on page 1/ 29

Object Oriented

Programming Using C++


UNIT 1- PRINCIPLES OF OBJECT ORIENTED PROGRAMMING
1.4 &1.5
Contents:
1. Special Operators in C++

2. Scope resolution Operator

3. Memory Management Operator

4. Manipulators

5. Structure of C++ program

6. Basic Input/ Output operators and functions in C++

7. Simple C++ Program.


Special Operators in C++:
• Special operators are the operators that are used to carried out some special
operations ,that ordinary operators such as arithmetic, assignment and
relationship operators not able to perform.

• Special operators are used for variety of applications.

• Types of Special Operators in C++:


Special Operators in C++

1. Scope resolution operator 2. Memory Management 3.Manipulators.


Operator
Scope resolution Operator(::)

• The scope resolution operator is used to reference the global variable or member function that is
out of the scope.

• The operator is represented as the double(::) double colon symbol.


In C++, the scope resolution operator is ::
It is used for the following purposes:

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>

int x=40; // Global variable x OUTPUT

int main() Global variable x is:40


{ Local variable x is:10
clrscr();
int x=10; //Local variable x
cout<<“ Global variable x is:”<<::x;
cout<<“Local variable x is:”<<x;
return 0;
};
2) To define a function outside a class.
C++ program to show that scope resolution operator :: is // used to define a function
outside a class
# include<iostream.h>
#include<conio.h>
OUTPUT
Class Student
{ Here fun is called outside the
public: class,which is the member function
void fun(); // only function is declared of the class Student.
};
// Definition outside class using ::
void Student::fun()
{

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

• It is used for dynamic memory allocation .

• This operator is used to create object at run time.

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

• Manipulators operators are:


1. endl
2. setw()
1.endl(line field operator):

• It has same effect as of”\n”.

• It is used to switch the control of the compiler to the new line.

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

• It requires <iomanip.h> header files. OUTPUT

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

cout << “Hello”


;
Object Insertion Variable
operator
Cout<< operator:

• This operator (<<) is called as an insertion operator.

• 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

• The operator “>>” is called as an Extraction 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++:

1. Formatted console Input and output functions

2. Unformatted console input output


1. Formatted console Input and output functions:

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

Include header files

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 .

• Most important header file is <iostream.h>

<iostream.h>: It allows input & output operations, such as reading


data from or writing the data to the screen .

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

commonly used functions of conio.h is:


clrscr() ;
getch();
2.Class Declaration :

• In this section the programmer will declare the classes which are
necessary for a given program

• Programmer uses general syntax for creating the classes.

• Syntax of declaring the class:

• class class_name
{
access_specifier:
data_members;
function _members;
};
3. Class Functions declaration :

• This section allow programmer to design member function of the class.

• Programmer can have inside or outside declaration of the function .

• There are 3 methods of defining member functions of a class:

1. Internally defined functions / Inside the class:


a. Nesting of Member function

2. Externally Defined Functions / Outside the class.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy