Oop Unit Ii
Oop Unit Ii
Oop Unit Ii
Friend function:
The private members of a class cannot be accessed from outside the class but in
some situations two classes may need access of each other’s private data.
The common function is made friendly with all those classes whose private data
need to be shared in that function. This common function is called as friend
function.
Characteristics:
• Friend function is not in the scope of the class to which it has been
declared as friend.
• It is called without any object of class like a normal function.
• It cannot access the member names directly and has to use an object name
and dot membership operator with each member name.
• It can be declared either in the public or the private part of a class without
affecting its meaning.
• It has the objects as arguments.
Example:
Program to interchange values of two integer numbers using friend function.
#include<iostream>
using namespace std;
class B;
class A
{
int x;
cin>>x;
};
class B
int y;
cin>>y;
};
void swap(A a, B b)
int temp;
temp =a.x;
a.x=b.y;
b.y=temp;
int main()
A a;
B b;
a.accept();
b.accept();
swap(a,b);
points)
State the difference between constructor and destructor. (any six 6 S-23
points)
class number
int x;
public:
number(int y)
x=y;
void display( )
};
int main( )
number n(50);
n.display( );
#include<conio.h>
class measure
private:
int add1,add2,add3;
public:
measure()
add1=10;
add2=20;
void sum()
add3=add1+add2;
void display()
cout<<"\nSum = "<<add3;
};
int main()
measure m1;
m1.sum();
m1.display();
getch();
return 0;
OR
#include<iostream>
#include<conio.h>
class measure
private:
public:
measure(int a,int b)
add1=a;
add2=b;
void sum()
void display()
cout<<"\nSum = "<<add3;
};
int main()
m.sum();
m.display();
getch();
return 0;
}
7. Write a program to declare class ‘employee’ containing data members ‘emp- 6 W-23
id’ and salary. Accept and display the data for 10 employee.
#include <iostream>
class employee {
private:
int emp_id;
double salary;
public:
salary = sal;
void displayData() {
cout << "Employee ID: " << emp_id << ", Salary: " << salary << endl;
};
int main()
Employee employees[10];
int empId;
double salary;
cout << "Enter details for Employee " << i + 1 << std::endl;
employees[i].setData(empId, salary);
employees[i].displayData();
return 0;
They belong to the class itself, not to specific objects of the class.
They can be accessed using the class name and the scope resolution
operator (::).
2. No this pointer
They can access static data members and other static member functions
of the class.
They can be invoked directly using the class name, even if no objects
of the class exist.
6. Cannot be virtual
They are often used for operations that are related to the class as a
whole, rather than to specific objects.
#include <iostream>
// Base class
class Base {
public:
Base() {
std::cout << "Base constructor\n";
}
};
// Derived class
class Derived : public Base {
public:
Derived() {
std::cout << "Derived constructor\n";
}
};
int main() {
Derived d; // Creating an object of the derived class
return 0;
}
10. Explain overloaded constructor with suitable example. 4 S-23
// Constructor overloading
#include <iostream>
class construct
public:
float area;
construct()
area = 0;
construct(int a, int b)
area = a * b;
void disp()
{
RSCOEP Department of Computer Engineering Prof.S.U.Puri 49
cout<< area<< endl;
};
int main()
{ // Constructor Overloading
// of class name
construct o;
o.disp();
o2.disp();
return 1;
11. Compare static and non-static data members. (any four points) 4 S-23
#include <iostream>
class Example {
public:
int a;
void add(Example E)
a = a + E.a;
};
// Driver Code
int main()
{ // Create objects
E1.a = 50;
E2.a = 100;
<< "\n\n";
// to function add()
E2.add(E1);
// object as argument
<< "\n\n";
return 0;
Constructors in C++ are the member functions that get invoked when
an object of a class is created.
1. Default
2. Parameterized
3. Copy constructors.
15. Develop a c++ program for accept data from user to calculate 4 W-22
#include <iostream>
int main() {
cin >> sub1 >> sub2 >> sub3 >> sub4 >> sub5;
// Calculate percentage
char grade;
grade = 'A+';
grade = 'A';
grade = 'B+';
grade = 'B';
grade = 'C';
grade = 'D';
grade = 'F';
// Display results
cout << "Percentage: " << percentage << "%" << endl;
return 0;
#include <iostream>
class Box {
private:
int length;
int breadth;
int height;
length = l;
breadth = b;
height = h;
};
int volume(Box b) {
int main() {
std::cout << "Volume of the box is: " << volume(box) << std::endl;
return 0;
17. Develop a c++ program for constructor with default argument and 6 W-22
use of destructor.
#include <iostream>
class MyClass {
public:
int x;
x = val;
cout << "Constructor called with value: " << x << endl;
// Destructor
~MyClass() {
cout << "Destructor called for object with value: " << x << endl;
};
int main() {
return 0;
Friend functions can access private and protected members of a class, unlike
ordinary functions which are restricted to public members. This ability to
bypass encapsulation is a key feature of friend functions.
19. Write a C++ program to declare a class student with data members as 4 S-22.S-19
#include <iostream>
#include <string>
class Student {
public:
int roll_no;
string name;
Student(int r, string n) {
roll_no = r;
name = n;
void display() {
};
int main() {
int r;
string n;
cin >> r;
return 0;
20. Write a C++ program to declare a class mobile having data members as 4 S-22
price and model number. Accept and display the data for Ten objects.
#include <iostream>
#include <string>
class Mobile {
public:
int price;
string model_number;
Mobile(int p, string m) {
price = p;
model_number = m;
void display() {
};
int main() {
Mobile mobiles[10];
string m;
cout << "Enter price for mobile " << i + 1 << ": ";
cin >> p;
cout << "Enter model number for mobile " << i + 1 << ": ";
cin >> m;
mobiles[i].display();
return 0;
class MyClass {
public:
};
Example:
#include <iostream>
class Rectangle {
public:
Rectangle(int l, int b = 5) {
length = l;
breadth = b;
int area() {
};
int main() {
return 0;
#include <iostream>
class Class2;
class Class1 {
private:
int m1;
public:
Class1(int m) : m1(m) {}
};
class Class2 {
private:
int m2;
public:
Class2(int m) : m2(m) {}
};
int main() {
Class1 obj1(80);
return 0;
23. Write any two characteristics of static data member. Write C++ 6 S-22
program to count number of objects created with the help of static data
member.
Initialized outside the class: A static data member must be defined and
initialized outside the class, usually in the global scope. This is because
static members belong to the class itself, not to individual objects.
#include <iostream>
class MyClass {
public:
MyClass() {
cout << "Number of objects created: " << count << endl;
};
int main() {
return 0;
Name: The destructor has the same name as the class, preceded by a
tilde (~). For example, for a class named MyClass, the destructor would
be ~MyClass().
#include <iostream>
class Rectangle {
public:
int length;
int breadth;
// Parameterized constructor
Rectangle(int l, int b) {
length = l;
breadth = b;
int area() {
};
int main() {
return 0;
‘stud_name’ and ‘roll_no’. Accept and display this data for 5 students.
#include <iostream>
#include <string>
class Student {
public:
string stud_name;
int roll_no;
stud_name = name;
roll_no = roll;
void display() {
};
int main() {
Student students[5];
string name;
int roll;
cout << "Enter student " << i + 1 << " name: ";
students[i].display();
return 0;
Characteristics:
Shared by all objects: All instances of the class share the same copy of the
static data member.
Initialized outside the class: Unlike regular data members, static data
members must be defined and initialized outside the class definition.
Accessed using class name: You can access a static data member using the
class name and the scope resolution operator (::).
Lifetime: The static data member's lifetime is the entire program's execution.
28. Write a C++ program to find smallest number from two numbers using 4 S-19
class ClassB;
class ClassA {
private:
int num1;
public:
ClassA(int n) : num1(n) {}
};
class ClassB {
private:
int num2;
public:
ClassB(int n) : num2(n) {}
};
int main() {
ClassA obj1(num1);
return 0;
29. Write a C++ program to declare a class student with members as roll 6 S-19
#include <iostream>
#include <string>
class Student {
public:
int roll_no;
string name;
string department;
roll_no = r;
name = n;
department = d;
void display() {
};
int main() {
int roll;
s1.display();
s2.display();
class student
{
int roll no;
char name [14];
} s[6];
void main( )
{ cout < <sixeof(s);
}
108
32. Describe use of static data member in C++ with example. 4 W-18
Characteristics:
33. Write a C++ program to find greatest number among two numbers 4 W-18
#include <iostream>
class ClassB;
class ClassA {
private:
int num1;
public:
ClassA(int n) : num1(n) {}
};
class ClassB {
private:
int num2;
public:
ClassB(int n) : num2(n) {}
};
int main() {
ClassA obj1(num1);
ClassB obj2(num2);
return 0;
34. Write a C++ program to accept array of five elements, find and display 4 W-18
#include <iostream>
int main() {
int arr[5];
smallest = arr[i];
return 0;