Uploaded by capgxssic

Vishal Goswami - PRACTICAL OOPs with c++

advertisement
OBJECT ORIENTED PROGRAMMING USING C++ LAB
PRACTICAL- 01
UNIT -01
1. Write a program to create 2 namespaces in a header file. Both the
namespaces should contain a variable "name" and a method "printHello()".
Access both these variables and methods from the main method.
SOURCE CODE:
#include <iostream>
// Declare first namespace
namespace first_namespace {
std::string name = "First namespace";
void printHello() {
std::cout << "Hello from the first namespace!" << std::endl;
}
}
// Declare second namespace
namespace second_namespace {
std::string name = "Second namespace";
void printHello() {
std::cout << "Hello from the second namespace!" << std::endl;
}
}
int main() {
// Access variables and methods from first namespace
std::cout << first_namespace::name << std::endl;
first_namespace::printHello();
// Access variables and methods from second namespace
std::cout << second_namespace::name << std::endl;
second_namespace::printHello();
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
2. Create a 1-d array and accept its elements from the user. Print the middle
element of the array. Also, reverse the array.
SOURCE CODE:
#include <iostream>
int main() {
// Declare array and variables
const int size = 5;
int array[size];
int middle = size / 2;
// Accept elements from the user
std::cout << "Enter " << size << " elements for the array: ";
for (int i = 0; i < size; i++) {
std::cin >> array[i];
}
// Print the middle element of the array
std::cout << "The middle element of the array is: " << array[middle] <<
std::endl;
// Reverse the array
for (int i = 0; i < middle; i++) {
int temp = array[i];
array[i] = array[size - i - 1];
array[size - i - 1] = temp;
}
// Print the reversed array
std::cout << "The reversed array is: ";
for (int i = 0; i < size; i++) {
std::cout << array[i] << " ";
}
std::cout << std::endl;
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
3.Create 2 strings in C++, concatenate these 2 strings and also compare them
lexicographically.
SOURCE CODE:
#include <iostream>
#include <string>
int main() {
// Declare and initialize strings
std::string str1 = "Hello";
std::string str2 = "World";
// Concatenate the strings
std::string str3 = str1 + " " + str2;
std::cout << "Concatenated string: " << str3 << std::endl;
// Compare the strings lexicographically
if (str1 == str2) {
std::cout << "The strings are equal." << std::endl;
} else {
std::cout << "The strings are not equal." << std::endl;
}
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
UNIT -02
1.Write a program to show the working of constructors and destructors in a
class. The object should be created dynamically from main method, initialized
from constructor and its memory should be released with the help of a
destructor.
SOURCE CODE:
#include <iostream>
class MyClass {
public:
// Constructor
MyClass() {
std::cout << "Constructor called." << std::endl;
}
// Destructor
~MyClass() {
std::cout << "Destructor called." << std::endl;
}
};
int main() {
// Create object dynamically
MyClass* p = new MyClass();
// Delete object and call destructor
delete p;
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
2.Illustrate how copy constructors can be used to perform deep copy.
SOURCE CODE:
#include <iostream>
#include <string>
class MyClass {
public:
std::string* str;
// Default constructor
MyClass() {
str = new std::string;
}
// Copy constructor
MyClass(const MyClass& other) {
str = new std::string;
*str = *other.str;
}
// Destructor
~MyClass() {
delete str;
}
};
int main() {
// Create original object
MyClass obj1;
*obj1.str = "Hello";
// Create copy of the object using the copy constructor
MyClass obj2 = obj1;
// Modify the original object
*obj1.str = "World";
// Print both objects
std::cout << "Original object: " << *obj1.str << std::endl;
std::cout << "Copy object: " << *obj2.str << std::endl;
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
3. Overload + operator to add 2 student objects.
SOURCE CODE:
#include <iostream>
class Student {
public:
std::string name;
int age;
// Default constructor
Student() {
name = "";
age = 0;
}
// Overloaded + operator
Student operator+(const Student& other) {
Student result;
result.name = name + " " + other.name;
result.age = age + other.age;
return result;
}
};
int main() {
// Create two student objects
Student s1;
s1.name = "simba";
s1.age = 21;
Student s2;
s2.name = "harley";
s2.age = 22;
// Add the two student objects using the overloaded + operator
Student s3 = s1 + s2;
// Print the result
std::cout << "Result: " << s3.name << ", " << s3.age << std::endl;
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
UNIT -03
1. Write a program to show the working of protected variable in inheritance.
SOURCE CODE:
#include <iostream>
// Base class
class Base {
protected:
int x;
};
// Derived class
class Derived: public Base {
public:
// Constructor
Derived(int val) {
x = val;
}
// Method to access protected variable
int getX() {
return x;
}
};
int main() {
// Create derived object
Derived d(1000);
// Access protected variable using method
std::cout << "x = " << d.getX() << std::endl;
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
2. Illustrate how diamond problem is resolved by use of virtual base class.
SOURCE CODE:
#include <iostream>
class A {
public:
A() { std::cout << "A's constructor called" << std::endl; }
};
class B : virtual public A {
public:
B() { std::cout << "B's constructor called" << std::endl; }
};
class C : virtual public A {
public:
C() { std::cout << "C's constructor called" << std::endl; }
};
class D : public B, public C {
public:
D() { std::cout << "D's constructor called" << std::endl; }
};
int main() {
D d;
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
3.Overload + operator to add 2 student objects.
SOURCE CODE:
#include <iostream>
class A {
public:
A() { std::cout << "A's constructor called" << std::endl; }
~A() { std::cout << "A's destructor called" << std::endl; }
};
class B : public A {
public:
B() { std::cout << "B's constructor called" << std::endl; }
~B() { std::cout << "B's destructor called" << std::endl; }
};
class C : public B {
public:
C() { std::cout << "C's constructor called" << std::endl; }
~C() { std::cout << "C's destructor called" << std::endl; }
};
int main() {
C c;
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
4.Create a parent class constructor and pass a value to it from child class
constructor.
SOURCE CODE:
#include <iostream>
// Parent class
class Parent {
public:
Parent(int value) {
this->value = value;
}
int getValue() {
return value;
}
private:
int value;
};
// Child class
class Child : public Parent {
public:
// Call the parent class constructor and pass a value to it
Child(int value) : Parent(value) {}
};
int main() {
Child child(30);
std::cout << child.getValue() << std::endl; // Outputs: 5
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
UNIT -04
1.Write a program to show the difference between method overloading and
overriding.
SOURCE CODE:
#include <iostream>
class BaseClass {
public:
void display(int x) {
std::cout << "BaseClass: Display method with one integer argument: " << x
<< std::endl;
}
void display(int x, int y) {
std::cout << "BaseClass: Display method with two integer arguments: " << x
<< ", " << y << std::endl;
}
};
class DerivedClass : public BaseClass {
public:
// Overriding the display method with one integer argument
void display(int x) override {
std::cout << "DerivedClass: Overridden display method with one integer
argument: " << x << std::endl;
}
// Overloading the display method with two integer arguments
void display(int x, int y) {
std::cout << "DerivedClass: Overloaded display method with two integer
arguments: " << x << ", " << y << std::endl;
}
};
int main() {
BaseClass bc;
bc.display(1000);
bc.display(1000, 2000);
DerivedClass dc;
dc.display(1000);
dc.display(1000, 2000);
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
return 0;
}
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
2.Write a program to create a virtual function inside a class and use it from
main method.
SOURCE CODE:
#include <iostream>
// Base class with a virtual function
class Base {
public:
virtual void greet() {
std::cout << "Hello from the base class whoa nice to meet you!" << std::endl;
}
};
// Derived class with a reimplementation of the virtual function
class Derived : public Base {
public:
void greet() {
std::cout << "Hello from the derived class did you met my base class!" <<
std::endl;
}
};
int main() {
Base* base = new Base();
base->greet(); // Outputs: "Hello from the base class!"
Derived* derived = new Derived();
derived->greet(); // Outputs: "Hello from the derived class!"
delete base;
delete derived;
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
3.Write a program to create a pure virtual function inside a class and use it
from main method.
SOURCE CODE:
#include <iostream>
// Parent class
class Parent {
public:
Parent(int value) {
this->value = value;
}
int getValue() {
return value;
}
private:
int value;
};
// Child class
class Child : public Parent {
public:
// Call the parent class constructor and pass a value to it
Child(int value) : Parent(value) {}
};
int main() {
Child child(60);
std::cout << child.getValue() << std::endl; // Outputs: 5
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
4.Create a virtual destructor in a class so that memory occupied by child
class object can be released.
SOURCE CODE:
#include <iostream>
class Shape {
public:
virtual ~Shape() { std::cout << "Shape's destructor called" << std::endl; }
};
class Circle : public Shape {
public:
~Circle() { std::cout << "Circle's destructor called" << std::endl; }
};
int main() {
Shape* shape = new Circle();
delete shape;
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
UNIT -05
1.Create a vector and traverse the vector using for-loop, for-each loop and
iterator.
SOURCE CODE:
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
// Traverse the vector using a for loop
std::cout << "Traversing the vector using a for loop:" << std::endl;
for (int i = 0; i < numbers.size(); i++) {
std::cout << numbers[i] << " ";
}
std::cout << std::endl;
// Traverse the vector using a for-each loop
std::cout << "Traversing the vector using a for-each loop:" << std::endl;
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
// Traverse the vector using an iterator
std::cout << "Traversing the vector using an iterator:" << std::endl;
for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); it++) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
2.Create 2 lists and swap the content of those lists.
SOURCE CODE:
#include <iostream>
#include <list>
int main() {
// Create two lists of integers
std::list<int> list1{6,7,8};
std::list<int> list2{1, 2, 3,4,5};
// Swap the contents of the lists
list1.swap(list2);
// Print the contents of the lists
std::cout << "List 1: ";
for (int x : list1) {
std::cout << x << " ";
}
std::cout << std::endl;
std::cout << "List 2: ";
for (int x : list2) {
std::cout << x << " ";
}
std::cout << std::endl;
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
3. Demonstrate exception handling using try, catch and throw keyword.
SOURCE CODE:
#include <iostream>
using namespace std;
int main() {
try {
// some code that might throw an exception
throw 20;
} catch (int e) {
// code to handle the exception
cout << "An exception occurred. Exception Nr. " << e << endl;
}
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
4. Write a program to read a file, and add "Bye" to the end of it.
SOURCE CODE:
#include <iostream>
#include <fstream>
#include <string>
int main() {
// Open the input file
std::ifstream input_file("abc.txt");
// Check if the file was opened successfully
if (!input_file.is_open()) {
std::cerr << "Error: Unable to open input file" << std::endl;
return 1;
}
// Read the contents of the file into a string
std::string contents((std::istreambuf_iterator<char>(input_file)),
std::istreambuf_iterator<char>());
// Add "Bye" to the end of the string
contents += "Bye\n";
// Close the input file
input_file.close();
// Open the output file
std::ofstream output_file("output.txt");
// Check if the file was opened successfully
if (!output_file.is_open()) {
std::cerr << "Error: Unable to open output file" << std::endl;
return 1;
}
// Write the modified contents of the file to the output file
output_file << contents;
// Close the output file
output_file.close();
return 0;
}
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
OBJECT ORIENTED PROGRAMMING USING C++ LAB
OUTPUT:
SHUBHAM MAITHANI / 21011795 / 3RD
SEMESTER / M
Download