CPP Unit 2 Edited

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

UNIT-2

1a) Define class structure in C++.


Answer:

b) Write briefly about access specifier/modifiers in C++.


Answer:
public - members are accessible from outside the class
private - members cannot be accessed (or viewed) from outside the class
protected - members cannot be accessed from outside the class, however, they can be accessed
in inherited classes. The protected members can be accessed by any class’s immediate derived
class.

c) Explain this keyword with an example program.


Answer:
The keyword this is pointer that refers to the current object.
Example:
class Point
{
private:
int x, y;
public:
Point(int x,int y)
{
this->x = x;
this->y = y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
Point p1(20,30);
cout<<"p1.x="<< p1.getX() <<endl;
cout<<"p1.y="<< p1.getY() <<endl;
return 0;
}

d) What is a constant member functions? Explain with an example program.


Answer:
The object called by these functions cannot be modified. It is recommended to use const
keyword so that accidental changes to object are avoided.
Example:
#include<iostream>
using namespace std;
class Test
{
int value;
public:
Test(int v = 0)
{
value = v;
}
int getValue() const
{
// value++; This function cant modify the object.
return value;
}
};
int main()
{
Test t(20);
cout<<t.getValue();
return 0;
}

2a) Write briefly about Friend keyword.


Answer:
The Friend keyword is used to permit the out side function or a class to get access to the private
members of any class.
b) Give the syntax of how to make a specific function of class as friend to another class.
Answer:
class B
{
private:
int b;
public:
B()
{
b = 10;
}
friend void A::showB(B &x);
};
In the above code, we made the showB() function of Class A as friend to Class B.

c) Explain Friend class with an example program.


Answer:
Friend Class A friend class can access private and protected members of other class in which it is
declared as friend.
Example:
#include <iostream>
using namespace std;
class A
{
private:
int a;
public:
A()
{
a=10;
}
friend class B; // Friend Class
};
class B
{
private:
int b;
public:
void showA(A &x)
{
cout << x.a;
}
};
int main()
{
A a;
B b;
b.showA(a);
return 0;
}

d) Explain Friend function with an example program.


Answer:
Friend Function Like friend class, a friend function can be given a special grant to access private
and protected members. A friend function can be:
a) A member of another class
b) A global function
Example:
#include <iostream>
using namespace std;
class A
{
int a;
public:
A()
{
a = 10;
}
friend void showA(A &x);
};
void showA(A &x)
{
cout<<x.a;
}
int main()
{
A a;
showA(a);
return 0;
}

3a) Differentiate between instance variable & static variable


Answer:
Instance variable: Every object will have a separate copy of value for all instance variables.
Static variable: There will be only one copy of value for any static variable, which will be
shared equally by all the objects.
b) Differentiate between instance(non static) function & static function.
Answer:
Instance function/Non-static function: The function which operate on instance variables of the
class.
Static function: These functions associated with the class. Do not operate on instance variable.
Can access static data.

c) Explain static data members with an example program.


Answer:
#include <iostream>
#include <string.h>
using namespace std;
class Account
{
public:
int accno;
char name[30];
static int count;

Account(int accno, char *name)


{
this->accno = accno;
strcpy(this->name, name);
count++;
}

void display()
{
cout<<accno<<" "<<name<<endl;
cout<<count;
}
};
int Account::count=0;
int main()
{
Account a1(201, "Sanjay");
Account a2(202, "Nakul");
Account a3(203, "Ranjana");
a1.display();
a2.display();
a3.display();
return 0;
}

d) Explain Static member functions with an example program.


Answer:
#include <iostream>
#include <string.h>
using namespace std;
class Account
{
private:
int accno;
char name[30];
static int count;
public:
Account(int accno, char *name)
{
this->accno = accno;
strcpy(this->name, name);
count++;
}
void display()
{
cout<<accno<<" "<<name<<endl;
}
static void showCount()
{
cout<<"\nNo.of Customers in this brach="<<count;
}
};
int Account::count=0;
int main()
{
Account a1(201, "Sanjay");
Account a2(202, "Nakul");
Account a3(203, "Ranjana");
a1.display();
a2.display();
a3.display();
Account::showCount();
return 0;
}

4a) Write briefly about constructor.


Answer:
Constructor is a special member function which will be called automatically whenever a new
object of the class is created.
Name of this function is same as that of the class name.
No return type
Generally used to initialize the newly created object.
Can be used to provide any resources to the newly created object.

b) What is a Default constructor?


Answer:
A constructor which has no argument is known as default constructor. It is invoked at the time of
creating object.
If we do not define any constructors in the class, the compiler will provide us the default
constructor.

c) Explain Constructor Overloading with example program.


Answer:
#include <iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point()
{
x = 0;
y = 0;
}
Point(int x,int y)
{
this->x = x;
this->y = y;
}
Point(int k)
{
x = k;
y = k;
}
void display()
{
cout<<x<<" "<<y;
}
};
int main()
{
Point p1(20,30);
p1.display();
Point p2;
p2.display()
Point p3(100);
p3.display();
return 0;
}

d)Explain Copy Constructor with example program.


Answer:
#include <iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x,int y)
{
this->x = x;
this->y = y;
}
Point(Point &p)
{
cout<<"\nCopy Constructor called\n";
x = p.x;
y = p.y;
}
void display()
{
cout<<x<<" "<<y;
}
};
int main()
{
Point p1(20,30);
p1.display();
Point p2(p1);
p2.display();
Point p3=p1;
p3.display();
return 0;
}

5a)Write briefly about destructor.


Answer:
Desctructor is a special member function which will be called automatically just before the
object’s destruction.
Name of this function is same as that of the class name and preceded with ‘~’ symbol.
No return type
Generally used to release any resources that were given to the object at construction time .

b) What is memory leak?


Answer:
// Assume the class Employee with 2 fields Empno, name.
int main()
{
Employee *ptr1 = new Employee(101,”shiva”);
Employee *ptr2 = new Employee(102,”kiran”);
ptr1->display();
ptr2->display();
ptr2=ptr1; // Now both pointer pointing to first object
ptr1->display();
ptr2->display();
delete ptr2;
}
In the above code, after the the assignment statement ptr2=ptr1, The second object have no
pointer pointing to it. So we cant delete the memory occupied by this object. This memory cant
be assigned by the OS to any other variable as long as the current program doesn’t come to an
end. This is called as memory leak.

c)Explain Destructor with example program.


Answer:
#include<iostream>
#include<string.h>
using namespace std;
class Student
{
private:
char *name;
int rno;
public:
Student(const char*,int);
~Student();
void displayStudent();
};
Student::Student(const char *s,int no)
{
cout<<"\nConstructor called"<<endl;
int len = strlen(s);
name = new char[len+1];
strcpy(name,s);
rno=no;
}
Student::~Student()
{
cout<<"\nDestructor called"<<endl;
delete[] name;
}
void Student::displayStudent()
{
cout<<"\nR.No="<<rno<<endl;
cout<<"\nName="<<name<<endl;
}
int main()
{
Student s1("shiva",1);
s1.displayStudent();
return 0;
}

d)Explain Dynamic Creation and Destruction of Objects.


Answer:
#include<iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"\nConstructor invoked";
}
void display()
{
cout<<"\nHello Welcome";
}
~Employee()
{
cout<<"\nDestructor invoked";
}
};
int main()
{
Employee *ptr = new Employee();
ptr->display();
delete ptr;
return 0;
}

6a)What is data hiding in C++.


Answer:
The private access modifier helps us to hide our class private data. These private
members(data/functions) can be accessed by other class only with the help of public functions
defined in that class.

b)Write briefly about abstraction.


Answer:
Hiding of implementation details(Function Definition) is called Abstraction.
Hiding irrelevant data and showing only the required data to the user.
Example: User can swith on the fan to rotate. Need not know the implementation mechanism like
what makes the fan to rotate.
c)Explain what is ADT with an example program.
Answer:
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of
value and a set of operations.
The definition of ADT only mentions what operations are to be performed but not how these
operations will be implemented.
It does not specify how data will be organized in memory and what algorithms will be used for
implementing the operations.
It is called “abstract” because it gives an implementation-independent view.
The process of providing only the essentials and hiding the details is known as abstraction.
Example:
Stack.h
Class Stack
{
private:
int st[10];
int top;
public:
Stack(){}
void push(int);
void pop();
void display();
};
Statck.cpp
#include “Stack.h”
Stack::Stack()
{
top=-1;
}
void Stack::puch(int x)
{
if(top==9)
cout<<”Overflow”;
else
{
top++;
st[top]=x;
}
}
void Stack::pop()
{
if(top==-1)
cout<<”Underflow”;
else
{
top++;
}
}
void Stack::display()
{
for(int i=0;i<=top;i++)
cout<<st[i];
}

In the above example stack.h file is the stack ADT. For the end user it is enough to know what
method are supported by the class Stack and the signatures of the methods of the class. So now
the user can create the object for the Stack class, invoke methods by passing required parameters.
The file Stack.cpp consists of the implementation details of each method which is not required
for the end use.

You might also like