0% found this document useful (0 votes)
30 views24 pages

Constructor

Constructor is a special type of member function that is automatically called when an object is created. It initializes the object and constructs values of data members. Constructors do not have a return type unlike normal member functions. The main types of constructors are default, parameterized, and copy constructors.

Uploaded by

kifiran496
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views24 pages

Constructor

Constructor is a special type of member function that is automatically called when an object is created. It initializes the object and constructs values of data members. Constructors do not have a return type unlike normal member functions. The main types of constructors are default, parameterized, and copy constructors.

Uploaded by

kifiran496
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Ques 6 Define constructor.

How is the constructor different from normal


member function?
Answer:
Constructor:

 A constructor is a member function of a class which initializes objects of


a class.
 A constructor in C++ is a special method that is automatically called
when an object of a class is created.
 It is called so because it constructs values of data members of the
class.
 In C++, it is automatically called when an object is created it is
considered to be a special member function of the class.
Constructor different from normal Member Function:

1. Constructor has same name as the class itself


2. It prepares the new object for use, often accepting arguments that the
constructor uses to set required member variables.
3. Function have return type but constructor don't have return type not
even void
4. If we do not specify a constructor, C++ compiler generates a default
constructor for us (expects no parameters and has an empty body).
5. Constructor is automatically called when we create the object of the
class while Member functions need to be called explicitly using the
object of the class.

Ques 7 What are the types of constructor?


Answer:
Types of Constructors are:

Default:
Default constructor is the constructor which doesn't take any argument. It has
no parameter.A default constructor is so important for initialization of object
members, that even if we do not define a constructor explicitly, the compiler
will provide a default constructor implicitly.
Example:

class c

public:

int s;

c()

s = 10;

};

int main()

c c1;

cout << c1.s;

return 0;

Parameterized:
These are the constructors with parameters.
Example:

class c

public:

int s;
c(int a)

s = a;

};

int main()

c c1(10);

c c2(37);

cout << c1.s;

cout << c2.s;

return 0;

Copy:
These are special type of Constructors which takes an object as argument,
and is used to copy values of data members of one object into another object.
Example:

class Stud

public:

int rollno;

string name;

Stud(int a)

rollno = a;

name = "";
}

Stud(int a, string s)

rollno = a;

name = s;

};

int main()

Stud A(10);

Stud B(11, "John");

return 0;

Ques 3 Explain private member function.


Answer:
A function declared inside the class's private section is known as a private
member function.
A private member function is accessible through the only public member
function.
The member functions can be accessed inside the class only if they are
private.
The access scope is limited to ensure the security of the private members and
their usage.
Example:

#include<iostream>

using namespace std;

class abc{
int a,b;

int p(int a,int b){ //p is a private member function can only be
accessed within the class

int c=a+b;

cout<<"Sum is:"<<c;

public:

int display(){

p(1,2);

};

int main(){

abc d;

d.display();

return(0);}

Output:

Sum is:3

Ques 4 Differentiate between public and private member function.


Answer:

Private Public
If data are declared as private The member functions with
in a class then it is accessible public access specifiers can
by the member functions of the be accessed outside of the
class where they are declared. class.

By default, any member of the


It is not by default,only public
class is considered as private
keyword can be used to
by the C++ compiler, if no
indicate public member
specifier is declared for the
functions and data items.
member.
A private member variable or A public member is accessible
function cannot be accessed, from anywhere outside the
or even viewed from outside class but within a program.
the class. Only the class and You can set and get the value
friend functions can access of public variables without any
private members. member.

Ques 19 Explain the term polymorphism with its types.


Answer:

 The word polymorphism means having many forms. Typically,


polymorphism occurs when there is a hierarchy of classes and they are
related by inheritance.
 Uses those methods to perform different tasks.
 It means that a call to a member function will cause a different function
to be executed depending on the type of object that invokes the
function.
Types of Polymorphism:

Compile Time:
This type of polymorphism is achieved by function overloading or operator
overloading.
Function Overloading:
When there are multiple functions with same name but different parameters
then these functions are said to be overloaded
Runtime polymorphism:
It is also known as dynamic polymorphism or late binding.
In runtime polymorphism, the function call is resolved at run time.
In contrast, to compile time or static polymorphism, the compiler deduces the
object at run time and then decides which function call to bind to the object.
Ques 24 Differentiate between virtual and pure virtual function.
Answer:
Virtual Function Pure Virtual Function
Member function declared
within the base class that
A virtual function in the base
can be redefined or
class with no implementation
overridden by a derived
class
A derived class that does A derived class that does not
not override a virtual override a pure virtual
function of the base class function of the base class
does not cause any causes any compilation
compilation errors. errors.
Base class does not have
Base class has the function
the function definition,it only
definition.
has function declaration.
There is at least one pure
There is no abstract class
virtual function,that class is
concept.
an abstract class.
Declaration:
Declaration: virtual get(int a)=0;
get(int a); // =0 is known as a pure
specifier.

Ques 22 Describe briefly about the term object.


Answer:
Object:

 An Object can be defined as an instance of a class.


 Objects provide a structured approach to programming
 An object contains an address and takes up some space in memory.
 It can include multiple properties and methods and may even contain
other objects.
 an object may refer to a single programming element, such as
a variable, constant, function, or method.
 Objects can communicate without knowing the details of each other's
data or code.
 Ques 20 Write a program to swap two numbers using pointers.
 Answer:

 #include <iostream>
 using namespace std;
 void swapValue(int *a,int *b);
 int main(){
 int a = 100;
 int b= 200;
 printf("First number is %d\n",a);
 printf("Second Number is %d\n",b);

 swapValue(&a,&b);
 printf("After swap function \n");
 printf("%d\n%d",a,b);
 }
 void swapValue(int *x,int *y){
 int temp;

 temp = *x;
 *x=*y;
 *y=temp;
 }

Ques 15 What are protected member? What is difference between public and
protected access modifier?
Answer:
Protected Member:
The protected keyword specifies access to class members in the member-list
up to the next access specifier or the end of the class definition.
A protected member variable or function is very similar to a private member
but it provides one additional benefit that they can be accessed in child
classes which are called derived classes.
Difference between Public and Protected access modifier:

 A protected member variable or function is very similar to a private


member but it provides one additional benefit that they can be accessed
in child classes which are called derived classes,protected members
cannot be accessed from outside the class, however, they can be
accessed in inherited classes.
 A public member is accessible from anywhere outside the class but
within a program. You can set and get the value of public variables
without any member.

Ques 1 Write short notes on objects and classes.


Answer:
Objects:

 An Object can be defined as an instance of a class.


 An object contains an address and takes up some space in memory.
 It can include multiple properties and methods and may even contain
other objects.
 Objects can communicate without knowing the details of each other's
data or code.
Classes:

 Collection of objects is called class.


 A class in C++ is the building block that leads to Object-Oriented
programming. It is a user-defined data type, which holds its own data
members and member functions, which can be accessed and used by
creating an instance of that class
 A class can also be defined as a blueprint from which you can create an
individual object.
 Class doesn't consume any space.
 Class declaration describes the type and scope of its members.
 It describes the scope and type of its members .
 For example:

#include <iostream>
#include <string>

using namespace std;

class item

public:

float oosd;

int dbms,cd,daa;

void getdata(float a,int b);

void putdata(void);

};

void item::getdata(float a,int b)

oosd=a;

dbms=b;

void item::putdata()

cout<<"DBMS: "<<dbms<<endl;

cout<<"OOSD: "<<oosd<<endl;

int main()

item i;

i.getdata(30.0,20);

i.putdata();

return 0;

}
Ques 2 Design a class using C++ to create a singly linked list.
Answer:
Program

#include <iostream>

using namespace std;

struct node

int data;

node *next;

};

class linked_list

private:

node *head,*tail;

public:

linked_list()

head = NULL;

tail = NULL;

void add_node(int n)

node *tmp = new node;

tmp->data = n;

tmp->next = NULL;

if(head == NULL)

head = tmp;

tail = tmp;
}

else

tail->next = tmp;

tail = tail->next;

};

int main()

linked_list a;

a.add_node(1);

a.add_node(2);

return 0;

Ques 14 What are the types of inheritance? Explain.


Answer:
Single Inheritance in C++
In this type of inheritance one derived class inherits from only one base
class. It is the simplest form of Inheritance.
Multiple Inheritance in C++
In this type of inheritance a single derived class may inherit from two or
more than two base classes.Multiple inheritance is the process of
deriving a new class that inherits the attributes from two or more
classes.
Hierarchical Inheritance in C++
In this type of inheritance, multiple derived classes inherit from a single
base class.Hierarchical inheritance is defined as the process of deriving
more than one class from a base class.
Multilevel Inheritance in C++
In this type of inheritance the derived class inherits from a class, which
in turn inherits from some other class. When one class inherits another
class which is further inherited by another class, it is known as multi
level inheritance in C++.
Multipath Inheritance in C++
Multipath inheritance in C++ is derivation of a class from other derived
classes, which are derived from the same base class. In this type of
inheritance, there involves other inheritance like multiple, multilevel,
hierarchical etc. It is famously known as the diamond problem in
computer programming.
Hybrid Inheritance in C++
Hybrid Inheritance is a combination of Hierarchical and Multilevel
Inheritance.
Ques 12 Explain type conversion with its type.
Answer:

 C++ allows us to convert data of one type to that of another.This is


known as type conversion.

Implicit Conversion:

 It is done automatically by the compiler and is known as implicit type


conversion,also known as automatic conversion.
 Generally takes place when in an expression more than one data type
is present.
 In such conditions type conversion (type promotion) takes place to
avoid loss of data.
 All the data types of the variables are upgraded to the data type of the
variable with the largest data type.
Explicit OR Type Casting:

 When the user manually changes data from one type to another, this is
known as explicit conversion, also known as type casting.
 Here the user can typecast the result to make it of a particular data
type.
 There are three major ways in which we can use explicit conversion in
C++.

Ques 10 Describe operator overloading.


Answer:
Operator overloading is an important concept in C++.

 It is a type of polymorphism in which an operator is overloaded to give


user defined meaning to it.
 Overloaded operators are functions with special names:
o the keyword "operator" followed by the symbol for the operator
being defined.
 Overloaded operator is used to perform operation on user-defined data
type.
 Example:

cout<<”Hello”; //here << is an overloaded insertion operator

Operator that cannot be overloaded are as follows:

 scope operator
 sizeof
 member selector
 member pointer selector
 ternary operator
Ques 11 What are the rules of operator overloading?
Answer:
Rules for operator overloading in C++

 Only built-in operators can be overloaded.


 The arity of the operators cannot be changed.
 We cannot overload operators for built-in data types.
 The precedence of the operators remains the same.
 The overloaded operator cannot hold the default parameters except
function call operator “()”.
Following are some restrictions to be kept in mind while implementing
operator overloading:

1. Precedence and Associativity of an operator cannot be changed.


2. Arity (numbers of Operands) cannot be changed. Unary operator
remains unary, binary remains binary etc.
3. No new operators can be created, only existing operators can be
overloaded.
4. Cannot redefine the meaning of a procedure. You cannot change how
integers are added.

Ques 8 Define destructor with its properties.


Answer:
Destructors:

 A destructor is a member function that is invoked automatically when


the object goes out of scope or is explicitly destroyed by a call to
delete.
 The destructor is called automatically by the compiler when the object
goes out of scope.
 The syntax for destructor is the same as that for the constructor, the
class name is used for the name of the destructor, with a tilde ~ sign as
prefix to it.
 Syntax:

class A

{
public:

// defining destructor for class

~A()

// statement

};

Properties of Destructor:

 Destructor function is automatically invoked when the objects are


destroyed.
 It cannot be declared static or const.
 Any class can have at most one destructor.
 The destructor does not have arguments.
 It has no return type, not even void.
 It does not receive any parameter, so cannot be overloaded.

Ques 9 When we call a destructor? How is it different from a normal member


function? Can there be more than one destructor in a class?
Answer:
A destructor function is called automatically when the object goes out of
scope:

 the function ends


 the program ends
 a block containing local variables ends
 a delete operator is called
Difference:

 Destructors have same name as the class preceded by a tilde (~)


 Destructors don’t take any argument and don’t return anything
No,there can only be 1 destructor in a class.

Ques 5 Explain static data and static function members.


Answer:
Static Data Member:

 When we declare a member of a class as static it means no matter how


many objects of the class are created, there is only one copy of the
static member.
 A static member is shared by all objects of the class.
 All static data are initialized to zero when the first object is created, if no
other initialization is present.
Static Member Functions:

 By declaring a function member as static, you make it independent of


any particular object of the class.
 A static member function can be called even if no objects of the class
exist and the static functions are accessed using only the class name
and the scope resolution operator ::.
 A static member function can only access static data members,other
static member functions and any other functions from outside the class.
 Static member functions have a class scope and they do not have
access to the pointer of the class.
 You could use a static member function to determine whether some
objects of the class have been created or not.
Example:

class s {

public:

static int a; //static data member

static int g() { //static member function

return a;

};
Ques 23 Explain this pointer with a help of example.
Answer:
The this pointer holds the address of the current object, in simple words you
can say that this pointer points to the current object of the class.
Example:

#include <iostream>

using namespace std;

class Pointer{

private:

int n;

char c;

public:

void get(int n, char c){

this->n=n;

this->c=c;

void display(){

cout<<"Number:"<<n<<endl;

cout<<"Character:"<<c;

};

int main(){

Pointer p1;

p1.get(1, 'C');

p1.display();

return 0;

}
Ques 17 Write a program in C++ to read in two matrices from the keyboard
and compute their sum.
Answer:

#include <iostream>

using namespace std;

int main()

int r, c, a[100][100], b[100][100], sum[100][100], i, j;

cout << "Enter number of rows (between 1 and 100): ";

cin >> r;

cout << "Enter number of columns (between 1 and 100): ";

cin >> c;

cout << endl << "Enter elements of 1st matrix: " << endl;

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

cout << "Enter element a" << i + 1 << j + 1 << " : ";

cin >> a[i][j];

cout << endl << "Enter elements of 2nd matrix: " << endl;

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

cout << "Enter element b" << i + 1 << j + 1 << " : ";

cin >> b[i][j];

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

sum[i][j] = a[i][j] + b[i][j];


cout << endl << "Sum of two matrix is: " << endl;

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

cout << sum[i][j] << " ";

if(j == c - 1)

cout << endl;

return 0;

Ques 13 Define inheritance. What are the modes of inheritance?


Answer:
Inheritance:
It is a process in which one object acquires all the properties and behaviors of
its parent object automatically.
In C++, the class which inherits the members of another class is called
derived class and the class whose members are inherited is called base class.
Modes of Inheritance:

1. Public mode: If we derive a subclass from a public base class. Then


the public member of the base class will become public in the derived
class and protected members of the base class will become protected in
the derived class.
2. Protected mode: If we derive a subclass from a Protected base class.
Then both public members and protected members of the base class
will become protected in the derived class.
3. Private mode: If we derive a subclass from a Private base class. Then
both public members and protected members of the base class will
become Private in the derived class.

Summary of Modes Of Inheritance mentioned above:


Base
Derived Classes→
Classes↓

Public Private Protected


Private ╳ ╳ ╳
Public Public Private Protected
Protected Protected Private Protected

Ques 18 What is virtual base class?


Answer:

 A virtual base class is a nested inner class whose functions and


member variables can be overridden and redefined by subclasses of an
outer class.
 Virtual classes are analogous to virtual functions.
 Virtual base classes are used in virtual inheritance in a way of
preventing multiple “instances” of a given class appearing in an
inheritance hierarchy when using multiple inheritances.
 The virtual base class is used when a derived class has multiple copies
of the base class.
Example Code:

class Bat {

public: int b;

};

class Dog : public Bat{

public: int d1;

};

class Puppy : public Bat {

public: int d2;

};

class Cow : public Dog, public Puppy {

public: int d3;

};

int main() {
Cow obj;

obj.d1 = 60;

obj.d2 = 70;

obj.d3 = 80;

cout<< "\n Dog : "<< obj.d1;

cout<< "\n Puppy: "<< obj.d2;

cout<< "\n Cow: "<< obj.d3;

Ques 16 Explain function overriding.


Answer:

 Function overriding is a feature that allows us to have the same


function in child class which is already present in the parent class.
o A child class inherits the data members and member functions of
parent class, but when you want to override a functionality in the
child class then you can use function overriding.
 When the base class and derived class have member functions with
exactly the same name, same return-type, and same arguments list,
then it is said to be function overriding.
 It enables you to provide specific implementation of the function which
is already provided by its base class.
Example:

class Animal {

public:

void eat(){

cout<<"Eating...";

};

class Dog: public Animal


{

public:

void eat()

cout<<"Eating bread...";

};

int main(void) {

Dog d = Dog();

d.eat();

return 0;

Ques 21 What is a pointer? How to use a pointer?


Answer:
Pointer:
A pointer is a variable whose value is the address of another variable, i.e.,
direct address of the memory location. Like any variable or constant, you must
declare a pointer before using it to store any variable address.
Syntax for using a pointer is:
int *a;
Here,int is the pointer's base type.
it must be a valid C data type and a is the name of the pointer variable.
The asterisk * used to declare a pointer is the same asterisk used for
multiplication. However, in this statement the asterisk is being used to
designate a variable as a pointer.

You might also like

pFad - Phonifier reborn

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

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


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy