Вы находитесь на странице: 1из 7

Q1:Explain memory management class B : virtual public A Q6:Explain parameterized constructor with

operators with the help of suitable { public: the help of suitable example.
example. int j; }; Ans: In C++, Constructor is automatically
Ans: There are two types of memory int main() called when the object(an instance of the
management operators in C++: 1new , { D ob; class) create.It is the special member function
2delete ob.i = 10; of the class.The constructor has arguments is
New operator: ob.j = 20; called as a Parameterized Constructor.
The new operator in C++ is used for ob.sum = ob.i + ob.j ; #include<iostream>
dynamic storage allocation. This cout << “Value of i is : ”<< ob.i<<”\n”; #include<conio.h>
operator can be used to create object of cout << “Value of j is : ”<< ob.j<<”\n”; using namespace std;
any type.example: cout << “Sum is : ”<< ob.sum <<”\n”; class Example {
1. int *a = new int; return 0; } int a, b;
2. *a = 20; Q4:Write a note on implicit and explicit type public:
or conversion. Example(int x, int y) {
3. int *a = new int(20); Automatic Conversion otherwise called as a = x;
the pointer variable a holds the address Implicit Conversion b = y;
of memory space allocated. This is not done by any conversions or cout << "Im Constructor\n";
delete operator: operators. In other words the value gets }
The delete operator in C++ is used automatically converted to the specific type to void Display() {
for releasing memory space when the which it is assigned. cout << "Values :" << a << "\t" << b;
object is no longer needed. 1. #include <iostream> }
Once a new operator is used, it is 2. using namespace std; };
efficient to use the corresponding delete 3. void main(){ int main() {
operator for release of memory. 4. short x=6000; Example Object(10, 20);
#include <iostream> void main() { int 5. int y; Object.Display();
*a= new int; *a=100; cout << " The 6. y=x;} getch();
Output is:a= " << *a; delete a; } Type casting otherwise called as Explicit return 0;
Q2: Explain use of default argument in a Conversion }
function with suitable Explicit conversion can be done using type Q7.Write a program to calculate area of a
example. cast operator and the general syntax for doing rectangle and a triangle using function
Ans: A default argument is a value this is : overloading.
provided in function declaration that is datatype (expression); Ans: #include<iostream.h>
automatically assigned by the compiler if Here in the above datatype is the type which #include<conio.h>
caller of the function doesn’t provide a the programmer wants the expression to gets const float pi=3.14;
value for the argument with default changed as. float area(float n,float b,float h)
value. In C++ the type casting can be done in either {
#include<iostream> of the two ways mentioned below namely: float ar;
using namespace std; C-style casting ar=n*b*h;
int sum(int x, int y, int z=0, int w=0) C++-style casting return ar;
{ return (x + y + z + w); } #include <iostream> }
int main() using namespace std; float area(float r)
{ cout << sum(10, 15) << endl; void main() {
cout << sum(10, 15, 25) << endl; {int a; float ar;
cout << sum(10, 15, 25, 30) << endl; float b,c; ar=pi*r*r;
return 0; } cout << "Enter the value of a:"; return ar;
Q3: What is friend function ? Write any cin >> a; }
three characteristics of it. cout << "Enter the value of b:"; float area(float l,float b)
Ans: A friend function of a class is cin >> b; {
defined outside that class' scope but it c = float(a)+b; float ar;
has the right to access all private and cout << "The value of c is:" << c;} ar=l*b;
protected members of the class. Q5Write a C++ program to calculate area and return ar;
characteristics: - A friend function is not circumference ofa circle using inline function. }
in the scope of the class, in which it has #include<iostream> void main()
been declared as friend. using namespace std; {
- It cannot be called using the object of float area(float); float b,h,r,l;
that class. float circum(float); float result;
- It can be invoked like a normal function int main() clrscr();
without any object. { int radius; cout<<“\nEnter the Base & Hieght of Triangle:
- Unlike member functions, it cannot use cout<<"\n Enter Radius of Circle: "; \n”;
the member names directly. cin>>radius; cin>>b>>h;
- It can be declared in public or private cout<<"\n Area of Circle : result=area(0.5,b,h);
part without affecting its meaning. "<<area(radius); cout<<“\nArea of Triangle: “<<result<<endl;
- Usually, it has objects as arguments. cout<<"\n Circumference of Circle : cout<<“\nEnter the Radius of Circle: \n”;
Q3:Explain virtual base class with a "<<circum(radius);} cin>>r;
suitable example. float area(float radius) result=area(r);
Ans: - An ambiguity can arise when { return (3.14 * radius * radius); cout<<“\nArea of Circle: “<<result<<endl;
several paths exist to a class from the } cout<<“\nEnter the Length & Bredth of
same base class. This means that a float circum(float radius) Rectangle: \n”;
child class could have duplicate sets of { cin>>l>>b;
members inherited from a single base return(2 * 3.14 * radius); result=area(l,b);
class. } cout<<“\nArea of Rectangle: “<<result<<endl;
class A else getch();}
{ public: ^ str[j++]=ch; }
int i; / fin.close();
}; cont.----/ return 0;}
Q8.Explain rules of operatoroverloading. Q11.Write a program to swap two Q14.Give the general format of class and
Ans: 1) Only built-in operators can be integers using function template. state the significance of private,public and
overloaded. New operators can not be Ans: #include<iostream> protected access specifire.
created.(2) Arity of the operators cannot using namespace std; Ans: Public, means all the class members
be changed.(3) Precedence and template <class T> declared under public will be available to
associativity of the operators cannot be void swap(T&a,T&b) //Function Template everyone. The data members and member
changed.(4) Overloaded operators { functions declared public can be accessed by
cannot have default arguments except T temp=a; other classes too.
the function call operator () which can a=b; class PublicAccess{
have default arguments.(5) Operators b=temp; public: // public access specifier
cannot be overloaded for built in types } int x; // Data Member Declaration
only. At least one operand must be used int main() void display(); // Member Function
defined type.(6) Assignment (=), { decaration}
subscript ([]), function call (“()”), and int x1=4,y1=7; Private keyword, means that no one can
member selection (->) operators must be float x2=4.5,y2=7.5; access the class members declared private
defined as member functions. cout<<“Before Swap:”; outside that class. By default class variables
Q9.Write a note on class template. cout<<“nx1=”<<x1<<“ty1=”<<y1; and member functions are private.
Ans: A class template provides a cout<<“nx2=”<<x2<<“ty2=”<<y2; class PrivateAccess{
specification for generating classes swap(x1,y1); private: // private access specifier
based on parameters. Class swap(x2,y2); int x; // Data Member Declaration
templates are generally used to cout<<“nnAfter Swap:”; void display(); // Member Function
implement containers. A class template cout<<“nx1=”<<x1<<“ty1=”<<y1; decaration}
is instantiated by passing a given set of cout<<“nx2=”<<x2<<“ty2=”<<y2; Protected, is the last access specifier, and it
types to it as template arguments. Here return 0; is similar to private, it makes class member
is an example of a class, MyTemplate, } inaccessible outside the class. But they can
that can store one element of any type Q12. Explain use of setfill( ) and setiosflags( ) be accessed by any subclass of that class.
and that has just one member manipulators with thehelp of suitable example. class ProtectedAccess{
function divideBy2, which divides its protected: // protected access specifier
Ans: std::setfill : Set fill character; Sets c as
value by 2. int x; // Data Member Declaration
the stream’s fill character. Behaves as if
template <class T> void display(); // Member Function
member fill were called with c as argument on
class MyTemplate { decaration}
the stream on which it is inserted as a
T element; Q15.what is function overloading?explain with
manipulator (it can be inserted on output
public: sutaible example.
MyTemplate (T arg) {element=arg;} Ans:Function overloading is a feature in C++
#include <iostream>
T divideBy2 () {return element/2;} where two or more functions can have the
#include <iomanip>
}; same name but different parameters.
int main()
Q10. Write a note on exceptionhandling. #include <iostream>
Ans: An exception is a problem that using namespace std;
std::cout << std::setfill('x') << std::setw(10);
arises during the execution of a void print(int i) {
std::cout << 77 << std::endl;
program. Exceptions provide a way to cout << " Here is int " << i << endl;
std::string str = "Geeks";
transfer control from one part of a }
std::cout << std::left << std::setfill('G') <<
program to another. C++ exception void print(double f) {
handling is built upon three cout << " Here is float " << f << endl;
std::cout << str << std::endl;
keywords: try, catch,and throw. }
return 0;
throw − A program throws an exception void print(char* c) {
when a problem shows up. This is done cout << " Here is char* " << c << endl;
using a throw keyword. Setiosflahs() }
catch − A program catches an exception When used in an int main() {
with an exception handler at the place in expression out << setiosflags(mask) or in >> print(10);
a program where you want to handle the setiosflags(mask), sets all format flags of the print(10.10);
problem. The catch keyword indicates stream out or in as specified by the mask. print("ten");
the catching of an exception. #include <iostream> return 0; }
try − A try block identifies a block of #include <iomanip> Q16.difference between memory
code for which particular exceptions will int main() management in c and cpp
be activated. It's followed by one or { std::cout << Ans: 1) c++ is Procedural, OOP, Generic
more catch blocks. std::resetiosflags(std::ios_base::dec) Programming while C is only Procedural
Q11.Write a program to display the << std::setiosflags( 2) In C++ references along with pointers are
contents of a text file in threverse order. std::ios_base::hex available but no references in C
Ans: #include std::ios_base::uppercase 3) In c++ Character arrays and Character
#include std::ios_base::showbase) << 42 << '\n';} objects are supported but no Character
#include Q13.Write a note on polymorphism. Objects in C
using namespace std; Ans: The word polymorphism means having 4) C++ Complex data types Include
int main() many forms. Typically, polymorphism occurs Structures, unions, classes but no classes in
{ when there is a hierarchy of classes and they C
ifstream fin("file_reverse_order.cpp"); are related by inheritance. 5) Multiple Class inheritance in C++ but no
char ch; C++ polymorphism means that a call to a Inheritance in C
int j=0; member function will cause a different function Q10cha ans. Syntax:
char str[100]; to be executed depending on the type of 1.try
while(!fin.eof()) object that invokes the function. In simple {//define program;
{ words, we can define polymorphism as the throw(variable);
fin.get(ch); ability of a message to be displayed in more }
if(ch==' '|| than one form. 2.Catch{
ch=='\n'||ch=='\r'||ch=='\t'||ch==',') //defines method to control error;}
{ 3.throw(variable);
Q16. explain copy constructor with Q19.Write sort note static data member. Q23.Explain the different ways to define
an example Ans: It is a variable which is declared with member function of a class with sutable
Ans: A copy constructor is a member the static keyword, it is also known as class example.
function which initializes an object using member, thus only single copy of the variable Ans:OUTSIDE THE CLASS: The member
another object of the same class. A copy creates for all objects. function is declared inside the class like a
constructor has the following general Any changes in the static data member normal function. This declaration informs the
function prototype: through one member function will reflect in all compiler that the function is a member of the
#include<iostream> other object’s member functions. class and that it has been defined outside the
using namespace std; Declaration class.
class Point static data_type member_name; class book
{ Defining the static data member {
private: It should be defined outside of the class II body of the class
int x, y; following this syntax: }:
public: data_type class_name :: member_name void book :: getdata(char a[],float b)
Point(int x1, int y1) { x = x1; y = y1; } =value; {
// Copy constructor If you are calling a static data member within a // defining member function outside the class
Point(const Point &p2) {x = p2.x; y = member function, member function should be strcpy(title,a):
p2.y; } declared as static price = b: }
int getX() { return x; } Q20.Explain with suitable example void book :: putdata ()
int getY() { return y; } operator overloading for unary operators { cout<<"\nTitle of Book: "<<title;
}; using friend function. cout<<"\nPrice of Book: "<<price; }
int main() Ans: Friend function using operator INSIDE THE CLASS: when a
{ overloading offers better flexibility to the class. member function is defined inside the class,
Point p1(10, 15); // Normal constructor These functions are not a members of the the class name and the scope resolution
is called here class and they do not have 'this' pointer. operator are not specified in the function
Point p2 = p1; // Copy constructor is When you overload a unary operator you have header. Moreover, the member functions
called here to pass one argument. defined inside a class definition are by default
// Let us access values assigned by #include<iostream> inline functions.
constructors using namespace std; class book
cout << "p1.x = " << p1.getX() << ", class UnaryFriend { char title[30];
p1.y = " << p1.getY(); { int a=10; int b=20; int c=30; float price;
cout << "\np2.x = " << p2.getX() << ", public: public:
p2.y = " << p2.getY(); void getvalues() void getdata(char [],float); II declaration
return 0; } {cout<<"Values of A, B & C\n"; void putdata()//definition inside the class
Q17.write and explain various cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<endl; } { cout<<"\nTitle of Book: "<<title;
functions used for unformatted void show() cout<<"\nPrice of Book: "<<price; } ;
console I/O operation. { cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<endl; Q23.what is scop resulation operator?give its
Ans: The I/O functions such as getch(), } syntax and explain any one application of it
putchar(), get(), and put() etc are called void friend operator-(UnaryFriend &x); with example.
unformatted console I/O functions. The }; Ans: Scope resolution operator (::) in C++
header file for these functions is and void operator-(UnaryFriend &x) programming language is used to define a
should be included in the beginning of {x.a = -x.a; function outside a class or when we want to
the program. The meaning and use of x.b = -x.b; use a global variable but also has a local
these functions can be illustrated as x.c = -x.c;} variable with the same name.
follows; int main() Eg.-> #include<iostream>
1.getch() – This function is used to { UnaryFriend x1; using namespace std;
accept the input character which is typed x1.getvalues(); int x; // Global x
by the keyboard during the execution of cout<<"Before Overloading\n"; int main()
C++ program. x1.show(); { int x = 10; // Local x
2.putchar() – It displays the character on cout<<"After Overloading \n"; cout << "Value of global x is " << ::x;
the screen at the current location of -x1; cout << "\nValue of local x is " << x;
cursor. x1.show(); return 0; }
3.get() and 4. put() are the string return 0;} Q24.Write a note on function tamplete
functions in C++ programming language. Q21.write a c++ program to perform Ans: In C++, function templates are functions
Q18.Explain the function used for multiplication and division operation on that serve as a pattern for creating other
manipulation of file pointers. two integer numbers by using inline similar functions. The basic idea behind
Ans: The C++ I/O system support for function. function templates is to create a function
function for setting a file pointer to any Ans: #include<iostream.h> without having to specify the exact type(s) of
desired position inside the file or to get #include<conio.h> some or all of the variables. Instead, we
the file pointer. #include<stdio.h> define the function using placeholder types,
The 1.seekp():Moves put file pointer to a inline float mul( float a, float b) called template type parameters.
specific location. And 2.tellp():Returns { return(a*b); #include <iostream>
the current position of the put pointer are } template <typename T>
member function of ofstream. The inline float div(float a, float b) const T& max(const T& x, const T& y)
3.seekg():Moves get file pointer to a { return(a/b) ; { return (x > y) ? x : y;
specific location or 4.tellg():Returns the } }
current position of the get pointer are int main () int main()
member function of ifstream. The class {float x,y; { int i = max(3, 7); // returns 7
fstream deals with file in both input and cout<<"enter the value of x:"; std::cout << i << '\n';
output modes. Hence, there are two file cin>>x; double d = max(6.34, 18.523);
pointer in class fastream and the put cout<<"enter the value of y:"; std::cout << d << '\n';
pointer used for writing and get pointer cin>>y; char ch = max('a', '6'); // returns 'a'
used for the reading. cout<<mul(x,y)<<"\n"; std::cout << ch << '\n';
cout<<div(x,y)<<"\n"; return 0;
getch(); }
Q27.Write a c++ program to read } Q27.What is constructor? Explain default
characters from a file.creat one file to protected respectively. constructor and copy constructor.
stor all upper case characters and Protected Inheritance : Ans:constructor: A constructor is a special
another file to store all lower case Public and Protected members are derived as type of member function that initialises
characters.also display contents of protected members. an object automatically when it is created.
both files. Explain the private, protected and public Compiler identifies a given member function
Ans: #include<fstream.h> inheritance. is a constructor by its name and the return
#include<stdio.h> Private Inheritance : All the public and type.Constructor has the same name as that
#include<ctype.h> protected members in base become private. of the class and it does not have any return
#include<string.h> Protected Inheritance : All the public type. Default Constructor: A default
#include<iostream.h> and protected members in base class become constructor is a constructor that either has no
#include<conio.h> protected. parameters, or if it has parameters, all the
void main() { Public Inheritance : In case of public parameters have default values.
char c, u; inheritance, public remains public and If no user-defined constructor exists for a
char fname[10]; protected remains protected.. class A and one is needed, the compiler
clrscr(); Q24.desigen a cpp classes which contain implicitly declares a default parameterless
ofstream out; function display().write a program to count constructor A::A(). This constructor is an
cout << "Enter File Name:"; number of times display() function is inline public member of its class.Copy
cin>>fname; called.(using static data member). Constructor: The copy constructor is a
out.open(fname); Ans: #include <iostream> constructor which creates an object by
cout << "Enter the text(Enter # at using namespace std; initializing it with an object of the same class,
end)\n"; //write contents to file class Counter which has been created previously. The copy
while ((c = getchar()) != '#') { { private: constructor is used to −
u = c - 32; //static data member as count 1.Initialize one object from another of the
out << u; static int count; same type.2.Copy an object to pass it as an
} public: argument to a function.
out.close(); //default constructor 3.Copy an object to return it from a function.
ifstream in(fname); //read the contents Counter() Q28.define file Expalin different ways to
of file { count++; } open a file.
cout << "\n\n\t\tThe File contains\n\n"; //static member function Ans:there are two way of opening and
while (in.eof() == 0) { static void Print() closing file:opening file def.: Once a stream
in.get(c); { has been created, next step is to associate a
cout << c; cout<<"\nTotal objects are: "<<count; file with it. And thereafter the file is available
} }; (opened) for processing.
getch(); } //count initialization with 0 Closing file def.: As already mentioned, a file
Q24.What are the rules for defining int Counter :: count = 0; is closed by disconnecting it with the stream it
virtual function. int main() is associated with. The close() function
Ans: 1.They Must be declared in public { Counter OB1; accomplishes this task and it takes the
section of class. OB1.Print(); following general form :syntax:
2.Virtual functions cannot be static and Counter OB2; stream_object.close();
also cannot be a friend function of OB2.Print(); 1.Opening File Using Constructors:
another class. Counter OB3; We know that a constructor of class initializes
3.Virtual functions should be accessed OB3.Print(); an object of its class when it (the object) is
using pointer or reference of base class return 0; } being created. Same way, the constructors of
type to achieve run time polymorphism. Q25.Write a cpp program to create a base stream classes (ifstream, ofstream, or
4.The prototype of virtual functions class increment. Write necessary member fstream) are used to initialize file stream
should be same in base as well as function to overloaded the operator unary pre objects with the filenames passed to them
derived class. & post increment ‘++’ for an integer number. 2.Opening Files Using Open() Function:
5.They are always defined in base class Ans:pre increment: A pre-increment There may be situations requiring a program
and overridden in derived class. It is not operator is used to increment the value of a to open more than one file. The strategy for
mandatory for derived class to override), opening multiple files depends upon how they
variable before using it in a expression
in that case base class version of #include <iostream> will be used. If the situation requires
function is used. simultaneous processing of two files, then you
using namespace std;
6.A class may have virtual destructor but need to create a separate stream for each
int main()
it cannot have a virtual constructor. { int x = 10, a; file.
Q27.define inheritance.Explain the Q1->11.what is reference variable
a = ++x;
visibility scope of private,public & Ans: - A reference variable is just like a
cout << "Pre Increment Operation";
protected access specifire. pointer with a few differences.
cout << "\na = " << a;
ans:Inheritance: In object-oriented - It is declared using '&' operator.
programming, inheritance is the concept - A reference variable must be initialized.
cout << "\nx = " << x;
that when a class of objects is defined, return 0; } - The reference variable once defined to refer
any subclass that is defined can inherit to a variable cannot be changed to point to
the definitions of one or more general post increment: A post-increment operator other variable.
classes. This means for the programmer is used to increment the value of variable after - You cannot create an array of references
that an object in a subclass need not executing expression completely in which post the way it is possible with pointer.
carry its own definition of data and increment is used - It is mostly used for function argument lists
methods that are generic to the class of #include <iostream> and function return values.
which it is a part. using namespace std; Q12.write two way opening file
Private Inheritance : int main() Ans: 1.Using the constructor function of the
The Public and protected members of { int x = 10, a; stream class.
Base class become private members of a = x++; 2.Using the function open().
the derived class. cout << "Post Increment Operation";
Public Inheritance : conti- cout << "\na = " << a;
All the public members and protected a=x++;
members are inherited as public and cout << "\nx = " << x; return 0; }
Q28.Write a cpp program to find area Q5.define class and encapsulation. Q9.Define abstraction
of triangle,circle and rectangle using Ans:1 class: Class: The building block of C++ Ans: Data abstraction is one of the most
function overloading. that leads to Object Oriented programming is essential and important feature of object
Ans:#include<iostream.h> a Class. It is a user defined data type, which oriented programming in C++. Abstraction
#include<conio.h> holds its own data members and member means displaying only essential information
const float pi=3.14; functions, which can be accessed and used by and hiding the details. Data abstraction refers
float area(float n,float b,float h) { creating an instance of that class. A class is to providing only essential information about
float ar; like a blueprint for an object. the data to the outside world, hiding the
ar=n*b*h; 2.Encapsulaton: In normal background details or implementation.
return ar; } terms Encapsulation is defined as wrapping Q10.what is abstract class
float area(float r) { up of data and information under a single unit. Ans: An abstract class is a class that is
float ar; In Object Oriented Programming, designed to be specifically used as a base
ar=pi*r*r; Encapsulation is defined as binding together class. An abstract class contains at least
return ar; } the data and the functions that manipulates one pure virtual function. You declare a pure
float area(float l,float b) { them. virtual function by using a pure specifier (= 0)
float ar; Q6.what is stream Enlist various stream in the declaration of a virtual member function
ar=l*b; classes. in the class declaration.
return ar; } Ans: The stream is the central concept of the The following is an example of an abstract
void main() { iostream classes. You can think of a stream class:
float b,h,r,l; object as a smart file that acts as a source and class AB {
float result; destination for bytes. A stream's public:
clrscr(); characteristics are determined by its class and virtual void f() = 0; };
cout<<“\nEnter the Base & Hieght of by customized insertion and extraction Q11.what is inline function
Triangle: \n”; operators. ans: C++ inline function is powerful concept
cin>>b>>h; Stream classes:1.ios stream:contain basic that is commonly used with classes. If a
result=area(0.5,b,h); facilities that are used by all other input and function is inline, the compiler places a copy
cout<<“\nArea of Triangle: output classes.2.istream(input of the code of that function at each point
“<<result<<endl; stream):declares input functions such as where the function is called at compile time.
cout<<“\nEnter the Radius of Circle: \n”; get(),getline(),and read().3.ostream(otput To inline a function, place the
cin>>r; stream): declares output functions put(),and keyword inline before the function name and
result=area(r); write().4.iostream(input output define the function before any calls are made
cout<<“\nArea of Circle: “<<result<<endl; stream):inherits the properties of ios stream to the function.
cout<<“\nEnter the Length & Bredth of and oistream through multiple inheritance and Q12.what is the perpose of setw and endl
Rectangle: \n”; thus contain all the input output function. Ans: setw:
cin>>l>>b; 5.streambuf:provides an interface to physical This manipulator changes the width of the
result=area(l,b); Device through buffer. next input/output field. When used in an
cout<<“\nArea of Rectangle: Q6.define const member function. expression out << setw(n) or in >> setw(n),
“<<result<<endl; Ans: A function becomes const when const sets the width parameter of the stream out or
getch(); } keyword is used in function’s declaration. The in to exactly n.
idea of const functions is not allow them to #include <iostream>
Question 1 answer: modify the object on which they are called. It #include <iomanip>
1.What is inheritance & types: is recommended practice to make as many int main() {
Ans:In C++, we have 5 different types of functions const as possible so that accidental std::cout << "no setw:" << 42 << '\n'
Inheritance. Namely, changes to objects are avoided. << "setw(6):" << std::setw(6) << 42
1.Single Inheritance << '\n'
int getValue() const {return value;}
2.Multiple Inheritance Q7.list the situations where inline function << "setw(6), several elements: " <<
3.Hierarchical Inheritance dosent work. 89 << std::setw(6) << 12 << 34 << '\n';
4.Multilevel Inheritance }
Ans: For functions returning values,if a loop,a
5.Hybrid Inheritance switch,or a goto exists. Endl:
Q2.is there a need to call a This manipulator has the same functionality
For functions not returning values,if a return
constructor function
statement exits. as ‘\n’(newline character). But this also
explicitely?justify. flushes the output stream.
If functions contain static variables.
Ans:yes: is there a need to call a #include<iostream>
If inline functions are recursive.
constructor function explicitely. Q7.define eof() function int main() {
Q3.What is this ponter. std::cout << "Hello" << std::endl <<
Ans: End-of-File
Ans: The ‘this’ pointer is passed as a "World!"; }
It is used as the value returned by several
hidden argument to all nonstatic member Q9.define constructor.
functions in header <cstdio> to indicate that
function calls and is available as a local the End-of-File has been reached or to signal Ans:A constructor is a special type of
variable within the body of all nonstatic
some other failure conditions. member function that initialises
functions. ‘this’ pointer is a constant Q9.what is free store operator.list free an object automatically when it is created.
pointer that holds the memory address store operation. Compiler identifies a given member function
of the current object. ‘this’ pointer is not
available in static member functions as Ans: FreestoreThe unary is a constructor by its name and the return
operators new and delete are available to type.Constructor has the same name as that
static member functions can be called
manipulate free store, which is a system- of the class and it does not have any return
without any object
provided memory pool for variables whose type. Also, the constructor is always public.
Q4.Class tamplet.
lifetime is directly managed by the Q10.what is pure virtual function.
Ans: Template is simple and yet very
programmer. Ans: A constructor is a special type of
powerful tool in C++. The simple idea is
You create the variable by using new and member function that initialises
to pass data type as a parameter so that
an object automatically when it is created.
we don’t need to write same code for destroy the object by using delete.
Manipulating free store memory is important Compiler identifies a given member function
different data types. C++ adds two new
for dynamic data structures such as lists and is a constructor by its name and the return
keywords to support
type.Constructor has the same name as that
templates: ‘template’ and ‘typename’. trees. We'll look at new and delete in the next
two lessons. of the class and it does not have any returnty
The second keyword can always be
replaced by keyword ‘class’.
Q12.What is fstream and data type Q18.List the operators that cannot be 24what is stream concept in cpp.
Ans:1.fstream: overloaded in cpp. Ans:Streams in C++ and Stream Classes. ...
This data type represents the file stream Ans: 1> Scope Resolution Operator (::) This process of flow of data is also known as
generally, and has the capabilities of 2> Pointer-to-member Operator (.*) encapsulation through streams. The data is
both ofstream and ifstream which means 3> Member Access or Dot operator (.) received from the keyboard or disk and can
it can create files, write information to 4> Ternary or Conditional Operator (?:) be passed to the monitor or to the disk. Figure
files, and read information from files. 5> Object size Operator (sizeof) describes the concept ofstream with input and
3ifstream: 6> Object type Operator (typeid) output devices.
This data type represents the input file 19.what is inheritance? State its 25 What is Dereferencing operator
stream and is used to read information advantages. Ans: In computer programming,
from files. Ans: The capability of a class to derive a dereference operator, also known as an
4.ofstream: properties and characteristics from another indirectionoperator, operates on a pointer
This data type represents the output file class is called Inheritance. Inheritance is one variable and returns the location value, or l-
stream and is used to create files and to of the most important feature of Object value, that it points to in memory. In the C
write information to files. Oriented Programming. programming language, the
Q13.define object with example. Advantages: deference operatoris denoted with an asterisk
Ans: An Object is an instance of a Reusability - facility to use public methods of (*).
Class. When a class is defined, no base class without rewriting the same.
memory is allocated but when it is Extensibility - extending the base class logic 26. Explain any four formatted
instantiated (i.e. an object is created) as per business logic of the derived class. input/output functions.
memory is allocated Data hiding - base class can decide to keep
#include <bits/stdc++.h> some data private so that it cannot be altered
using namespace std; by the derived class
class Geeks Overriding -With inheritance, we will be able
{ public: to override the methods of the base class so
string geekname; that meaningful implementation of the base
void printname() { class method can be designed in the derived
cout << "Geekname is: " << geekname; class.
} }; Disadvantages:-
int main() { 1.One of the main disadvantages of
Geeks obj1; inheritance in Java is the increased time/effort
obj1.geekname = "Abhi"; it takes the program to jump through all the
obj1.printname(); levels of overloaded classes.
return 0; } 2.Main disadvantage of using inheritance is
Q15.Enlist user defined data types in that the two classes get tightly coupled.
cpp. This means one cannot be used independent
Ans: Primptive data type: of each other.
Integer 3.Also with time, during maintenance adding
Character new features both base as well as derived
Boolean classes are required to be changed.
Floating Point 4.If a method is deleted in the "super class" or
Double Floating Point aggregate, then we will have to re-factor in
Valueless or Void case of using that method.
Wide Character Q22.What is reference variable.
Q15.Difference betwn default and Ans: C++ References. Advertisements.
paramiterised constructor . A reference variable is an alias, that is,
Ans: Default constructor does not have another name for an already existing variable.
a parameters. Parameterized Once a reference is initialized with avariable,
constructor is having one or more either the variable name or
parameters. the reference name may be used to refer to
Default constructor is used to initialize the variable.
every object with same 23.list any four fetures oops.
data. Parameterized constructor is used Ans: Features
to initialized each object with different Shared with non-OOP predecessor
data. languages. ...
Q16.Why do we make a function pure Objects and classes. ...
virtual function. Class-based vs prototype-based. ...
Ans: Briefly, it's to make the class Dynamic dispatch/message passing. ...
abstract, so that it can't be instantiated, Encapsulation. ...
but a child class can override the pure Composition, inheritance, and delegation. ...
virtual methods to form a concrete class. Polymorphism. ...
This is a good way to define an interface Open recursion.
in C++. 24.what is generic pointer.
Q17.list varous classes available for Ans: When a variable is declared as being
file operation a pointer to type void it is known as ageneric
Ans: 1.ifstream 2.ofstream 3.istream pointer. Since you cannot have a variable of
4.ostream 5.iostream. type void, the pointer will not point to any data
Q18.justify:constructor can have and therefore cannot be dereferenced. It is still
different name as the class. a pointer though, to use it you just have to
Ans: Constructor – Constructors – A cast it to another kind of pointer first.
constructor is a member function of the
class and it is called each time when an
object of that class is created. It is called
constructor, because it constructs the
value of data members of the class.