C++ Imp Questions
C++ Imp Questions
// reference variable
int& ref = GFG;
It can never hold a null value as It can hold or point at a null value
it needs an existing value to and be termed as a nullptr or null
become an alias of pointer
Example of Function
Example of Operator Overloading:
Overloading:
1. int GFG() = X() + Y();
1. int GFG(int X, int Y);
2. int GFG() = X() – Y();
2. int GFG(char X, char Y);
The base class has a virtual A base class having pure virtual
function that can be function becomes abstract that cannot
represented or instanced; In be represented or instanced; In simple
simple words, its object can words, it means its object cannot be
be made. made
An
When should we use multiple inheritance?
Multiple inheritances mean that a derived class can inherit two or
more base/parent classes. It is useful when a derived class needs to
combine numerous attributes/contracts and inherit some, or all, of the
implementation from these attributes/contracts. To take a real-life
example consider your Parents where Parent A is your DAD Parent B
is your MOM and Chid C is you.
It is achieved by function
It is achieved by virtual functions
overloading and operator
and function overriding
overloading.
public:
};
B. Parameterized constructor: It is a type of constructor which accepts
arguments or parameters. It has to be called explicitly by passing
values in the arguments as these arguments help initialize an object
when it is created. It also has the same name as that of the class.
// parameterized constructors
#include
class GFG {
private:
int x, y;
public:
// Parameterized Constructor
x = x1;
y = y1;
};
int main()
// Constructor called
return 0;
Output
G.x = 10, G.y = 15
delete[] GFG;
single pointer
It is used for deleting the objects It is used for deleting the objects
of new[]; By this, we can say of new; By this, we can say
that delete[] is used to delete an that delete is used to delete a
array of objects single object
statements;
} class Class_2nd {
statements;
statements;
statements;
} OR class GFG {
statements;
#include
<stdio.h>
int fun(void)
printf("Geeksforgeeks");
return 0;
}
What is STL?
STL is known as Standard Template Library, it is a library that provides
4 components like container, algorithms, and iterators.
Syntax:
inline data_type function_name()
{
Body;
}
Example:
class ClassA
private:
int a;
public:
ClassA()
a=10;
}
friend class FriendClass;
};
class FriendClass
private:
int b;
public:
void printClassA(ClassA& p)
cout<<“a=”<<p.a<<endl;
};
int main()
ClassA x;
FriendClass y;
y.printClassA();
return 0;
}
How can we access data members and member functions of a
class?
Dot-Operator( . ) helps to access data members and member functions of a
class.
16. What are access specifiers? What are the different types?
Access Specifiers define the accessibility of the members of the class. There
are three types of access specifiers:
25. Private – they are only accessible from inside of the class
26. Protected – like private specifiers but with an extra feature —
they are also accessible in derived classes
27. Public – they are accessible from both inside and outside the
class
Request a demo
Abdul Bari
4.6 (21,672)
Learn C++ Programming -Beginner to Advance- Deep Dive in C++
Abdul Bari
4.6 (26,751)
Mastering Data Structures & Algorithms using C and C++
Abdul Bari
4.6 (49,417)
Bestseller
Abdul Bari
4.6 (5,008)
【AI 자막】 C 와 C++ 를 사용하여 데이터 구조 및 알고리즘 마스터하기
Abdul Bari
4.8 (17)
Courses by Abdul Bari
39. Can we initialize a vector with an array?
Yes, we can initialize a vector with an array.
42. Write a C++ program for finding the length of a string using a
string iterator.
# include<iostream>
string str="welcome";
string::iterator it;
int count=0;
for(it=str.begin();it!=str.end();it++)
cout<<"length is"<<count<<endl;
return 0;
43. Write a C++ program to change the given string to all upper
cases.
#include<iostream>
int main()
string str="wELcoMe7";
for(int i=0;str[i]!='\0';i++)
{
str[i]=str[i]-32;
cout<<str<<endl;
return 0;
# include<iostream>
int main()
string str="MADAM";
string rev="";
int len=(int)str.length();
rev.resize(len);
for(int i=0;j=len-1;i<len;i++;j--)
rev[i]=str[j];
rev[len]='\0';
if(str.compare(rev)==0)
cout<<"palindrome"<<endl;
else
cout<<"not a palindrome"<<endl;
return 0;
# include<iostream>
template<class t>
t maxim(t a,t b)
return a>b?a:b;
int main()
cout<<maxim(12,14)<<endl;
cout<<maxim(2.3,1.4)<<endl;
cout<<maxim(2.3f,5.6f)<<endl;
return 0;
class rectangle
public:
int length;
int breadth;
int area()
return length*breadth;
int perimeter()
};
int main()
rectangle r1;
rectangle *ptr;
ptr=&r1;
ptr->length=10;
ptr->breadth=5;
cout<<ptr->area()<<endl;
cout<<ptr->perimeter()<<endl;
# include<iostream>
class complex
private:
int real;
int img;
public:
complex(int r=0,i=0)
real=r;
img=i;
void display()
cout<<real<<"+i"<<img;
}
};
complex temp;
temp.real=c1.real+c2.real;
temp.img=c1.img+v2.img;
return temp;
int main()
complex c1(5,3),c2(10,5),c3;
c3=c1+c2;
c3.display();
# include<iostream>
class base
{
public:
int a;
void display()
cout<<"display of base"<<a<<endl;
};
public:
void show()
cout<<"show of derived"<<endl;
};
int main()
derived d;
d.a=100;
d.display();
d.show();
}
49. Write a C++ program showing inheritance.
# include<iostream>
class rectangle
private:
int length;
int breadth;
public:
void setlength(int l)
if(l>0)
length=l;
else
length=1;
if(b>0)
breadth=b;
else
breadth=1;
}
int getlength()
return length;
int getbreadth()
return breadth;
int area()
return length*breadth;
int perimeter()
return 2*(length+breadth);
};
int main()
rectangle r1;
r1.setlength(10);
r1.setbreadth(5);
cout<<"r1.area()<<endl;
cout<<"r1.perimeter()<<endl;
cout<<"length"<<r1.getlength()<<endl;
cout<<"breadth"<<r1.getbreadth()<<endl;
# include<iostream>
class car
public:
cout<<"car started"<<endl;
};
public:
void start()
{
cout<<"innova started"<<endl;
};
public:
void start()
cout<<"swift started"<<endl;
};
int main()
ptr->start();
p=new swift();
ptr->start();
# include<iostream>
using namespace std;
class outer
class inner;
public:
void fun()
i.display();
class inner
public:
void display()
cout<<"display of inner"<<endl;
};
inner i;
};
int main()
outer::inner i;
}
# include<iostream>
if(b==0)
throw 1;
return a/b;
int main()
int x=10,y=2,z;
try
if(y==0)
throw 1;
z=x/y;
cout<<z<<endl;
catch(int e)
{
cout<<"division by zero"<<e<<endl;
cout<<"bye"<<endl;
# include<vector>
int main()
vector<int> v={2,4,6,8,10};
v.push_back(20);
v.push_back(30);
vector<int>::iterator itr;
cout<<"using iterator"<<endl;
for(itr=v.begin();itr!=v.end();itr++)
cout<<++*itr<<endl;
for(int x:v)
cout<<x<<endl;
}
54. Write a C++ program using map from STL.
# include<iostream>
# include<map>
int main()
map<int,string> m;
m.insert(pair<int,string>(1,"john"));
m.insert(pair<int,string>(2,"ravi"));
m.insert(pair<int,string>(3,"khan"));
map<int,string>::iterator itr;
for(itr=m.begin();itr!=m.end();itr++)
cout<<itr->first<<" "<<itr->second<<endl;
map<int,string>::iterator itr1;
itr1=m.find(2);
cout<<itr1->first<<" "<<itr1->second<<endl;
float fun()
return 2.34f;
int main()
double d=12.3f;
int i=9;
auto x=2*d+i;
cout<<x;
# include<iostream>
int main()
[](int x,int y)
cout<<"sum is "<<x+y<<endl;
}
(10,30);
#include<iostream>
#include<cstdarg>
va_list list;
va_start(list,n);
int x;
int s=0;
for(int i=0;i<n,i++)
x=va_arg(list,int);
s+=x;
return s;
int main()
{
cout<<sum(3,10,20,30)<<endl;
cout<<sum(5,1,2,3,4,5)<<endl;
# include<iostream>
# include<fstream>
int main()
ofstream ofs("my.text",ios::trunc);
ofs<<"john"<<endl;
ofs<<25<<endl;
ofs<<"cs"<<endl;
ofs.close();
# include<iostream>
# include<fstream>
ifstream ifs;
ifs.open("my.txt");
if(ifs_open())
cout<<"file is opened"<<endl;
string name;
int roll;
string branch;
ifs>>name>>roll>>branch;
ifs.close();
cout<<"name"<<name<<endl;
cout<<"roll"<<rollendl;
cout<<"branch"<<branch<<endl;