SlideShare une entreprise Scribd logo
1  sur  8
What is function overloading? Write a c++ program to implement a function overloading.
Answer
More than one user defined functions can have same name and perform different operations this feature of
c++ is known as function overloading. Every overloaded function should however have a different prototype.
To find area of square, rectangle and circle
#include< iostream.h>
#include< conio.h>
int area(int);
int area(int,int);
float area(float);
void main()
{
clrscr();
cout< < " Area Of Square: "< < area(4);
cout< < " Area Of Rectangle: "< < area(4,4);
cout< < " Area Of Circle: "< < area(3.2);
getch();
}
int area(int a)
{
return (a*a);
}
int area(int a,int b)
{
return(a*b);
}
float area(float r)
{
return(3.14 * r * r);
}
Explain about the constructors and Destructors with suitable example
Answer
Constructors and Destructors:
Constructors are member functions of a class which have a same name as a class name. Constructors are
called automatically whenever an object class is created. Whereas destructors are also the member functions
with the same name as class, they are invoked automatically whenever object life expires , therefore it is
used to return memory back to the system if the memory was dynamically allocated. Generally the
destructor function is needed only when constructor has allocated dynamic memory. Destructors are
differentiated by prefix tilde (~) from constructors.
Constructor and destructors are usually defined as public members of their class and may never possess a
return value. Constructs can be overloaded whereas destructors cannot be overloaded.
Example : constructor
class myclass
{
private: int a; int b;
public:
myclass()
{a=10;
b=10;
}
int add(void)
{return a+b;
}
};
void main(void)
{myclass a;
cout<<a.add();
}
Example Destructor
#include<iostream.h>
class myclass
{public:
~myclass()
{cout<<"destructedn";
}
};
void main(void)
{myclass obj;
cout<<"inside mainn
}
Explain about polymorphism. Explain its significance
Answer
Polymorphism means the ability to take more than one form. Defined as feature in c++ where operator or
function behaves differently depending upon what they are operating on. The behavior depends on the data
types used in the operation. Polymorphism is extensively used in implementing Inheritance.
The operator + will be adding two numbers when used with integer variables. However when used with user
defined string class, + operator may concatenate two strings. Similarly same functions with same function
name can perform different actions depending upon which object calls the function. Operator overloading is
a kind of polymorphism.
In C++, polymorphism enables the same program code calling different functions of different classes
Example:
In below codewWe are using a common code as following so that you can draw several of these shapes with
same code and the shape to be drawn is decided during runtime:
Shape *ptr[100]
For (int j=0;j<n;j++)
Ptr[j]->draw();
As in the above code if ptr pointing to rectangle then rectangle is drawn and if it points to circle then circle is
drawn.
What is an inheritance? Explain different types of inheritance
Answer
Inheritance allows one data type to acquire characteristics and behavior of other data types this feature of
inheritance allows easy modification of existing code and also programmer can reuse code without modifying
the original one which saves the debugging and programming time and effort. It can be defined as process of
creating a new class called derived class from the existing or base class. Thus the child class has all the
functionality of the parent class and has additional features of its own. Inheritance from a base class may be
declared as public, protected, or private.
Types of Inheritance:
Inheritance are of five types as follows
1) Single inheritance
It has only one base class and one derived class
2) Multilevel inheritance
In multilevel inheritance another class is derived from the derived class
3) Multiple Inheritance
There are two base classes and one derived class which is derived from both two base classes.
4) Hierarchical inheritance
In this there are several classes derived from a single base class.
5) Hybrid inheritance
Combination of any above mentioned inheritance types.
Write a c++ program to implement the relational operator overloading for the distance
class
Answer
#include <iostream>
using namespace std;
class Distance
{private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
// method to display distance
void displayDistance()
{cout << "F: " << feet << " I:" << inches <<endl;
}
// overloaded minus (-) operator
Distance operator- ()
{feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
// overloaded < operator
bool operator <(const Distance& d)
{if(feet < d.feet)
{return true;
}
if(feet == d.feet && inches < d.inches)
{return true;
}
return false;
}
};
int main()
{Distance D1(11, 10), D2(5, 11);
if( D1 < D2 )
{cout << "D1 is less than D2 " << endl;
}
else
{cout << "D2 is less than D1 " << endl;
}
return 0;
}
Create a class String which stores a string value. Overload ++ operator which converts
the characters of the string to uppercase (toupper() library function of “ctype.h” can be
used).
Answer
# include<iostream.h>
# include<ctype.h>
# include<string.h>
# include<conio.h>
class string
{ char str[25];
public:
string()
{ strcpy(str, “”);}
string(char ch[])
{ strcpy(str, ch);}
void display()
{ cout<<str;}
string operator ++()
{string temp;
int i;
for(i=0;str[i]!=’0′;i++)
temp.str[i]=toupper(str[i]);
temp.str[i]=’0′;
return temp;
}
};
void main()
{ clrscr();
string s1=”hello”, s2;
s2=s1++;
s2.display();
getch();
}
What is a virtual function? Explain it with an example
Answer
A virtual function is a member function that is declared within a base class and redefined by a derived class.
To create virtual function, precede the function’s declaration in the base class with the keyword virtual.
When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its
own needs.
Virtual means existing in effect but not in reality. Virtual functions are primarily used in inheritance.
Class A
{
int a;
public:
A()
{
a = 1;
}
virtual void show()
{
cout <<a;
}
};
Class B: public A
{
int b;
public:
B()
{
b = 2;
}
virtual void show()
{
cout <<b;
}
};
int main()
{
A *pA;
B oB;
pA = &oB;
pA->show();
return 0;
}
Output is 2 since pA points to object of B and show() is virtual in base class A.
Explain different access specifiers in a class
Answer
Access specifiers control access to class members. Or access specifiers in C++ determine the scope of the
class members.
A common set of access specifiers that c++ supports are as follows:
Private:
Restricts the access to the class itself therefore none of the external function can access the private data and
member functions. Therefore if a class member is private, it can be used only by the members and friends of
class.
Public:
Public means that any code can access the member by its name. Therefore If a class member is public, it can
be used anywhere without the access restrictions.
Protected:
The protected access specifier restricts access to member functions of the same Class, or those of derived
classes.
Define a STUDENT class with USN, Name, and Marks in 3 tests of subject. Declare an array of 10 STUDENT
objects. Using appropriate functions, find the average of two better marks for each student. Print the USN,
Name and the average marks of all the student
Answer
#include<iostream.h>
#include<conio.h>
class student
{
private:
char usn[10];
float avg;
char name[30];
int test[3];
public:
void get_stud_details()
{
cout<<"Enter the USN number: ";
cin>>usn;
cout<<"Enter the name of the student: ";
cin>>name;
cout<<"Enter the marks of three test: n";
for(int i=0;i<3;i++)
{
cin>> test[i];
}
}
void net_avg()
{
int m,j,k;
avg=0;
for( j=0;j<2;j++)
{
for(int k=j+1;k<3;k++)
{
if(test[j]>test[k])
{
m=test[j];
test[j]=test[k];
test[k]=m;
}
}
}
avg=(test[1]+test[2])/2.0;
}
void display()
{
cout<<"|******Student details are******|"<<endl;
cout<<"Student USN:"<<usn<<endl;
cout<<"Student name:"<<name<<endl;
cout<<"The best of two from the three marks"<<endl;
for( int i=1;i<3;i++)
cout<<test[i]<<endl;
cout<<"nAverage:"<<avg<<endl;
}
};
void main()
{
int n;
student s[100];
clrscr();
cout<<"Enter the number of students:";
cin>>n;
for(int i=0;i<n;i++)
{
s[i].get_stud_details();
s[i].net_avg();
}
for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}
Write a C++ program to create a template function for quick sort and demonstrate sorting of integers
Answer
#include<iostream.h>
#include<conio.h>
template<class T>
int partition( T a[],int low,int high )
{T num=a[low];
int i=low+1;
int j=high;
T temp;
while( 1 )
{while( i<high && num>a[i] )
i++;
while( num<a[j] )
j--;
if( i<j )
{temp=a[i];
a[i]=a[j];
a[j]=temp;
}else
{temp=a[low];
a[low]=a[j];
a[j]=temp;
return(j);
}
}
}
template<class T>
void Quick(T a[],int low,int high )
{int j;
if( low<high )
{j=partition(a,low,high);
Quick(a,low,j-1);
Quick(a,j+1,high );
}
}
void main()
{int N;
clrscr();
cout<<"Enter array size : ";
cin>>N;
int *p=new int[N];
cout<<"nEnter "<<N<<" int no's..n";
for(int i=0; i<N; i++ )
{cin>>p[i];
}
cout<<"nArray before sorting is..n";
for(i=0; i<N; i++ )
cout<<p[i]<<"n";
Quick( p,0,N-1 );
cout<<"nArray after sorting is..n";
for(i=0; i<N; i++ )
cout<<p[i]<<"n";
getch();
}

Contenu connexe

Tendances

Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Anton Kolotaev
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011Deepak Singh
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and stringsRai University
 
CBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question PaperCBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question PaperMalathi Senthil
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++Fahim Adil
 
Strings in c
Strings in cStrings in c
Strings in cvampugani
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsMayank Jain
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Deepak Singh
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cppgourav kottawar
 

Tendances (20)

Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
C++ theory
C++ theoryC++ theory
C++ theory
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 
Templates
TemplatesTemplates
Templates
 
CBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question PaperCBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question Paper
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
14 strings
14 strings14 strings
14 strings
 
Strings in c
Strings in cStrings in c
Strings in c
 
C sharp chap4
C sharp chap4C sharp chap4
C sharp chap4
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Strings
StringsStrings
Strings
 
String in c
String in cString in c
String in c
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 

En vedette

Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsRai University
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 augshashank12march
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSaharsh Anand
 
Nesting of for loops using C++
Nesting of for loops using C++Nesting of for loops using C++
Nesting of for loops using C++prashant_sainii
 
Oo ps concepts in c++
Oo ps concepts in c++Oo ps concepts in c++
Oo ps concepts in c++Hemant Saini
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)Way2itech
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programmingAshita Agrawal
 
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...cprogrammings
 
The logic gate circuit
The logic gate circuitThe logic gate circuit
The logic gate circuitroni Febriandi
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
COMPUTER ORGANIZATION - Logic gates, Boolean Algebra, Combinational Circuits
COMPUTER ORGANIZATION - Logic gates, Boolean Algebra, Combinational CircuitsCOMPUTER ORGANIZATION - Logic gates, Boolean Algebra, Combinational Circuits
COMPUTER ORGANIZATION - Logic gates, Boolean Algebra, Combinational CircuitsVanitha Chandru
 

En vedette (18)

Program flow control 2
Program flow control 2Program flow control 2
Program flow control 2
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
Flow control in c++
Flow control in c++Flow control in c++
Flow control in c++
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Nesting of for loops using C++
Nesting of for loops using C++Nesting of for loops using C++
Nesting of for loops using C++
 
Oo ps concepts in c++
Oo ps concepts in c++Oo ps concepts in c++
Oo ps concepts in c++
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
The logic gate circuit
The logic gate circuitThe logic gate circuit
The logic gate circuit
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
COMPUTER ORGANIZATION - Logic gates, Boolean Algebra, Combinational Circuits
COMPUTER ORGANIZATION - Logic gates, Boolean Algebra, Combinational CircuitsCOMPUTER ORGANIZATION - Logic gates, Boolean Algebra, Combinational Circuits
COMPUTER ORGANIZATION - Logic gates, Boolean Algebra, Combinational Circuits
 

Similaire à Bc0037

Similaire à Bc0037 (20)

OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
C++ language
C++ languageC++ language
C++ language
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Oop concepts
Oop conceptsOop concepts
Oop concepts
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Ch.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objectsCh.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objects
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
EEE 3rd year oops cat 3 ans
EEE 3rd year  oops cat 3  ansEEE 3rd year  oops cat 3  ans
EEE 3rd year oops cat 3 ans
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 

Dernier

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 

Dernier (20)

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 

Bc0037

  • 1. What is function overloading? Write a c++ program to implement a function overloading. Answer More than one user defined functions can have same name and perform different operations this feature of c++ is known as function overloading. Every overloaded function should however have a different prototype. To find area of square, rectangle and circle #include< iostream.h> #include< conio.h> int area(int); int area(int,int); float area(float); void main() { clrscr(); cout< < " Area Of Square: "< < area(4); cout< < " Area Of Rectangle: "< < area(4,4); cout< < " Area Of Circle: "< < area(3.2); getch(); } int area(int a) { return (a*a); } int area(int a,int b) { return(a*b); } float area(float r) { return(3.14 * r * r); }
  • 2. Explain about the constructors and Destructors with suitable example Answer Constructors and Destructors: Constructors are member functions of a class which have a same name as a class name. Constructors are called automatically whenever an object class is created. Whereas destructors are also the member functions with the same name as class, they are invoked automatically whenever object life expires , therefore it is used to return memory back to the system if the memory was dynamically allocated. Generally the destructor function is needed only when constructor has allocated dynamic memory. Destructors are differentiated by prefix tilde (~) from constructors. Constructor and destructors are usually defined as public members of their class and may never possess a return value. Constructs can be overloaded whereas destructors cannot be overloaded. Example : constructor class myclass { private: int a; int b; public: myclass() {a=10; b=10; } int add(void) {return a+b; } }; void main(void) {myclass a; cout<<a.add(); } Example Destructor #include<iostream.h> class myclass {public: ~myclass() {cout<<"destructedn"; } }; void main(void) {myclass obj; cout<<"inside mainn } Explain about polymorphism. Explain its significance Answer Polymorphism means the ability to take more than one form. Defined as feature in c++ where operator or function behaves differently depending upon what they are operating on. The behavior depends on the data types used in the operation. Polymorphism is extensively used in implementing Inheritance. The operator + will be adding two numbers when used with integer variables. However when used with user defined string class, + operator may concatenate two strings. Similarly same functions with same function name can perform different actions depending upon which object calls the function. Operator overloading is a kind of polymorphism. In C++, polymorphism enables the same program code calling different functions of different classes
  • 3. Example: In below codewWe are using a common code as following so that you can draw several of these shapes with same code and the shape to be drawn is decided during runtime: Shape *ptr[100] For (int j=0;j<n;j++) Ptr[j]->draw(); As in the above code if ptr pointing to rectangle then rectangle is drawn and if it points to circle then circle is drawn. What is an inheritance? Explain different types of inheritance Answer Inheritance allows one data type to acquire characteristics and behavior of other data types this feature of inheritance allows easy modification of existing code and also programmer can reuse code without modifying the original one which saves the debugging and programming time and effort. It can be defined as process of creating a new class called derived class from the existing or base class. Thus the child class has all the functionality of the parent class and has additional features of its own. Inheritance from a base class may be declared as public, protected, or private. Types of Inheritance: Inheritance are of five types as follows 1) Single inheritance It has only one base class and one derived class 2) Multilevel inheritance In multilevel inheritance another class is derived from the derived class 3) Multiple Inheritance There are two base classes and one derived class which is derived from both two base classes. 4) Hierarchical inheritance In this there are several classes derived from a single base class. 5) Hybrid inheritance Combination of any above mentioned inheritance types. Write a c++ program to implement the relational operator overloading for the distance class Answer #include <iostream> using namespace std; class Distance {private: int feet; // 0 to infinite int inches; // 0 to 12 public: // required constructors Distance(){ feet = 0; inches = 0; } Distance(int f, int i){ feet = f; inches = i; } // method to display distance void displayDistance() {cout << "F: " << feet << " I:" << inches <<endl; }
  • 4. // overloaded minus (-) operator Distance operator- () {feet = -feet; inches = -inches; return Distance(feet, inches); } // overloaded < operator bool operator <(const Distance& d) {if(feet < d.feet) {return true; } if(feet == d.feet && inches < d.inches) {return true; } return false; } }; int main() {Distance D1(11, 10), D2(5, 11); if( D1 < D2 ) {cout << "D1 is less than D2 " << endl; } else {cout << "D2 is less than D1 " << endl; } return 0; } Create a class String which stores a string value. Overload ++ operator which converts the characters of the string to uppercase (toupper() library function of “ctype.h” can be used). Answer # include<iostream.h> # include<ctype.h> # include<string.h> # include<conio.h> class string { char str[25]; public: string() { strcpy(str, “”);} string(char ch[]) { strcpy(str, ch);} void display() { cout<<str;} string operator ++() {string temp; int i; for(i=0;str[i]!=’0′;i++) temp.str[i]=toupper(str[i]); temp.str[i]=’0′; return temp; }
  • 5. }; void main() { clrscr(); string s1=”hello”, s2; s2=s1++; s2.display(); getch(); } What is a virtual function? Explain it with an example Answer A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function’s declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs. Virtual means existing in effect but not in reality. Virtual functions are primarily used in inheritance. Class A { int a; public: A() { a = 1; } virtual void show() { cout <<a; } }; Class B: public A { int b; public: B() { b = 2; } virtual void show() { cout <<b; } }; int main() { A *pA; B oB; pA = &oB; pA->show(); return 0; } Output is 2 since pA points to object of B and show() is virtual in base class A.
  • 6. Explain different access specifiers in a class Answer Access specifiers control access to class members. Or access specifiers in C++ determine the scope of the class members. A common set of access specifiers that c++ supports are as follows: Private: Restricts the access to the class itself therefore none of the external function can access the private data and member functions. Therefore if a class member is private, it can be used only by the members and friends of class. Public: Public means that any code can access the member by its name. Therefore If a class member is public, it can be used anywhere without the access restrictions. Protected: The protected access specifier restricts access to member functions of the same Class, or those of derived classes. Define a STUDENT class with USN, Name, and Marks in 3 tests of subject. Declare an array of 10 STUDENT objects. Using appropriate functions, find the average of two better marks for each student. Print the USN, Name and the average marks of all the student Answer #include<iostream.h> #include<conio.h> class student { private: char usn[10]; float avg; char name[30]; int test[3]; public: void get_stud_details() { cout<<"Enter the USN number: "; cin>>usn; cout<<"Enter the name of the student: "; cin>>name; cout<<"Enter the marks of three test: n"; for(int i=0;i<3;i++) { cin>> test[i]; } } void net_avg() { int m,j,k; avg=0; for( j=0;j<2;j++) { for(int k=j+1;k<3;k++) { if(test[j]>test[k])
  • 7. { m=test[j]; test[j]=test[k]; test[k]=m; } } } avg=(test[1]+test[2])/2.0; } void display() { cout<<"|******Student details are******|"<<endl; cout<<"Student USN:"<<usn<<endl; cout<<"Student name:"<<name<<endl; cout<<"The best of two from the three marks"<<endl; for( int i=1;i<3;i++) cout<<test[i]<<endl; cout<<"nAverage:"<<avg<<endl; } }; void main() { int n; student s[100]; clrscr(); cout<<"Enter the number of students:"; cin>>n; for(int i=0;i<n;i++) { s[i].get_stud_details(); s[i].net_avg(); } for(i=0;i<n;i++) { s[i].display(); } getch(); } Write a C++ program to create a template function for quick sort and demonstrate sorting of integers Answer #include<iostream.h> #include<conio.h> template<class T> int partition( T a[],int low,int high ) {T num=a[low]; int i=low+1; int j=high; T temp; while( 1 ) {while( i<high && num>a[i] )
  • 8. i++; while( num<a[j] ) j--; if( i<j ) {temp=a[i]; a[i]=a[j]; a[j]=temp; }else {temp=a[low]; a[low]=a[j]; a[j]=temp; return(j); } } } template<class T> void Quick(T a[],int low,int high ) {int j; if( low<high ) {j=partition(a,low,high); Quick(a,low,j-1); Quick(a,j+1,high ); } } void main() {int N; clrscr(); cout<<"Enter array size : "; cin>>N; int *p=new int[N]; cout<<"nEnter "<<N<<" int no's..n"; for(int i=0; i<N; i++ ) {cin>>p[i]; } cout<<"nArray before sorting is..n"; for(i=0; i<N; i++ ) cout<<p[i]<<"n"; Quick( p,0,N-1 ); cout<<"nArray after sorting is..n"; for(i=0; i<N; i++ ) cout<<p[i]<<"n"; getch(); }