0% found this document useful (0 votes)
28 views5 pages

Practice Sheet OOPs-2

The document is a practice sheet for Object-Oriented Programming (OOP) in C++, covering key concepts such as reference, dynamic memory allocation, inline functions, function overloading, default arguments, constructors and destructors, friend functions and classes, and the 'this' pointer. Each topic includes a C++ program example demonstrating the concept. The document serves as a guide for understanding and implementing various OOP principles in C++.

Uploaded by

nothinga921
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)
28 views5 pages

Practice Sheet OOPs-2

The document is a practice sheet for Object-Oriented Programming (OOP) in C++, covering key concepts such as reference, dynamic memory allocation, inline functions, function overloading, default arguments, constructors and destructors, friend functions and classes, and the 'this' pointer. Each topic includes a C++ program example demonstrating the concept. The document serves as a guide for understanding and implementing various OOP principles in C++.

Uploaded by

nothinga921
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/ 5

Programming Sheet-2 (OOPs)

Practice Sheet Object Oriented Programming


Unit-2
Topics: Concept of reference, dynamic memory allocation using new and delete operators,
inline functions, function overloading, function with default arguments, constructors and
destructors, friend function and classes, using this pointer.
1. Concept of Reference
Task: Write a C++ program that swap two numbers using a call by reference.
#include <iostream>
using namespace std;
void swap(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}
int main() {
int a = 10, b = 20;
cout << "Before swap: a = " << a << ", b = " << b << endl;
swap(a, b);
cout << "After swap: a = " << a << ", b = " << b << endl;
return 0;
}

2. Dynamic Memory Allocation using new and delete


Task: Write a C++ program that explains the concept of dynamic memory allocation in c++
using the new and delete keywords.
#include <iostream>
using namespace std;
int main() {
int *ptr = new int; // dynamically allocate memory for an integer
*ptr = 42;
cout << "Value: " << *ptr << endl;
delete ptr; // free dynamically allocated memory
return 0;
}

Page 1 of 5
Programming Sheet-2 (OOPs)

3. Inline Functions
Task: Create a C++ program to define an inline functions.
#include <iostream>
using namespace std;
inline int square(int x) {
return x * x;
}
int main() {
cout << "Square of 5: " << square(5) << endl;
cout << "Square of 8: " << square(8) << endl;
return 0;
}

4. Function Overloading
Task: Explain the concept of function overloading using a C++ program.
#include <iostream>
using namespace std;
void display(int x) {
cout << "Integer: " << x << endl;
}
void display(double x) {
cout << "Double: " << x << endl;
}
void display(string x) {
cout << "String: " << x << endl;
}
int main() {
display(10);
display(3.14);
display("Hello");
return 0;
}

5. Function with Default Arguments


Task: Explain the concept of default arguments with the help of C++ program.
#include <iostream>
using namespace std;

Page 2 of 5
Programming Sheet-2 (OOPs)

void greet(string name = "User") {


cout << "Hello, " << name << "!" << endl;
}

int main() {
greet(); // default argument will be used
greet("Alice");
return 0;
}

6. Constructor and Destructor


Task: WAP to explain the working of constructors and destrutures
#include <iostream>
using namespace std;
class Person {
public:
Person() { // Constructor
cout << "Constructor called!" << endl;
}
~Person() { // Destructor
cout << "Destructor called!" << endl;
}
};
int main() {
Person obj; // Constructor is called
return 0; // Destructor is called automatically at end of scope
}

7. Friend Function
Task: WAP to explain how a friend function can access the private members of the class
#include <iostream>
using namespace std;
class Box {
int width;
public:
Box() : width(10) {}
friend void printWidth(Box box); // Friend function declaration
};
Page 3 of 5
Programming Sheet-2 (OOPs)

void printWidth(Box box) {


cout << "Width of box: " << box.width << endl; // Access private member
}
int main() {
Box b;
printWidth(b);
return 0;
}

8. Friend Class
Task: WAP for friend class.
#include <iostream>
using namespace std;
class B;
class A {
int data;
public:
A() : data(10) {}
friend class B; // Declaring friend class
};
class B {
public:
void display(A &obj) {
cout << "Value of A's data: " << obj.data << endl; // Access private data
}
};
int main() {
A a;
B b;
b.display(a);
return 0;
}

9. Using this Pointer


Task: Explain this pointer using a program.
#include <iostream>
using namespace std;
class Student {

Page 4 of 5
Programming Sheet-2 (OOPs)

string name;
public:
Student(string name) {
this->name = name; // Using 'this' pointer to refer to class member
}
void display() {
cout << "Student's name: " << this->name << endl;
}
};
int main() {
Student s("John");
s.display();
return 0;
}

10. Dynamic Array Allocation using new


Taks: Explain Dynamic memory allocation in C++.
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;

int* arr = new int[size]; // Dynamically allocate array

for(int i = 0; i < size; i++) {


arr[i] = i + 1;
}
cout << "Array elements: ";
for(int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
delete[] arr; // Deallocate memory
return 0;
}

Page 5 of 5

You might also like