SlideShare a Scribd company logo
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();
}

More Related Content

What's hot

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++ 2011
Deepak Singh
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
Carson Wilber
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
Divyanshu Dubey
 
C++ theory
C++ theoryC++ theory
C++ theory
Shyam Khant
 
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
Rai University
 
Templates
TemplatesTemplates
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
Malathi Senthil
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++Fahim Adil
 
Oops presentation
Oops presentationOops presentation
Oops presentation
sushamaGavarskar1
 
Strings in c
Strings in cStrings in c
Strings in c
vampugani
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
Mayank Jain
 
C++ Programming
C++ ProgrammingC++ Programming
Strings
StringsStrings
Strings
Nilesh Dalvi
 
String in c
String in cString in c
String in c
Suneel Dogra
 
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 cpp
gourav kottawar
 

What's hot (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
 

Viewers also liked

Program flow control 2
Program flow control 2Program flow control 2
Program flow control 2
mrutherfordwest
 
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
Rai University
 
Flow control in c++
Flow control in c++Flow control in c++
Flow control in c++
Subhasis Nayak
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav 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 & destructor
Saharsh 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
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
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 programming
Ashita 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 Circuits
Vanitha Chandru
 

Viewers also liked (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
 

Similar to Bc0037

OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
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
Muhammed Thanveer M
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
C++ language
C++ languageC++ language
C++ language
Hamza Asif
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
shubhra chauhan
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
rehan16091997
 
3 functions and class
3   functions and class3   functions and class
3 functions and classtrixiacruz
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
Pranali Chaudhari
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
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 operatorSAFFI Ud Din Ahmad
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
Docent Education
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
SURBHI SAROHA
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
hara69
 
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
ITNet
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
WondimuBantihun1
 
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
Karthik Venkatachalam
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 

Similar to 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
 

Recently uploaded

H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 

Recently uploaded (20)

H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 

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