Assignment Prac
Assignment Prac
Q2)
#include <iostream>
#include <cstring>
using namespace std;
class first {
private:
char* str1;
char* str2;
public:
first(const char* s1 = "", const char* s2 = "") {
str1 = new char[strlen(s1) + 1];
strcpy_s(str1, strlen(s1) + 1, s1);
str2 = new char[strlen(s2) + 1];
strcpy_s(str2, strlen(s2) + 1, s2);
}
first(const first& n) {
str1 = new char[strlen(n.str1) + 1];
strcpy_s(str1, strlen(n.str1) + 1, n.str1);
void disp() {
cout << "str1: " << str1 << ", str2: " << str2 << endl;
}
~first() {
cout << "Destructor called" << endl;
delete str1;
delete str2;
str1 = nullptr;
str2 = nullptr;
cout << "Destructor done" << endl;
}
};
int main() {
first f1("hello", "Adwyait");
f1.disp();
first f2 = f1;
f2.disp();
first f3;
f3 = f1;
f3.disp();
return 0;
}
Q3)
#include<iostream>
#include<cstring>
using namespace std;
class Voter{
private:
int id;
char* Name;
char* Address;
public:
Voter():id(0),Name(nullptr),Address(nullptr){
}
Voter(const Voter& ref){
id=ref.id;
Name = new char[strlen(ref.Name)+1];
strcpy_s(Name,strlen(ref.Name)+1,ref.Name);
Address = new char[strlen(ref.Address)+1];
strcpy_s(Address,strlen(ref.Address)+1,ref.Address);
}
Voter& operator=(const Voter& ref) {
if (this != &ref) {
delete[] Name;
delete[] Address;
id = ref.id;
Name = new char[strlen(ref.Name) + 1];
strcpy_s(Name, strlen(ref.Name) + 1, ref.Name);
void input() {
cout << "\nEnter Voter ID: ";
cin >> id;
cin.ignore();
~Voter() {
delete[] Name;
delete[] Address;
cout << "Destructor called for ID: " << id << endl;
}
};
int main(){
int n;
cout << "How many voters? ";
cin >> n;
cin.ignore();
delete[] Voters;
return 0;
}
Q4)
#include<iostream>
using namespace std;
class Sample {
private:
int num;
public:
Sample() :num(0) {
cout << "Default Constructor called" << endl;
}
Sample(int n) :num(n) {
cout << "Parameterized Constructor called with value: " << num << endl;
}
Sample(const Sample& ref) {
num = ref.num;
}
void display() const {
cout << "Value of num: " << num << endl;
}
Sample operator=(const Sample& ref) {
if (this != &ref) {
num = ref.num;
}
return *this;
}
Sample operator+(const Sample& ref){
Sample temp;
temp.num = this->num + ref.num;
return temp;
}
~Sample() {
cout << "Destructor called for num: " << num << endl;
}
};
Q5)
Online C++ compiler to run C++ program online
/*
5) RTTI
virtual or pure virtual function
collect child class addresses in parent pointer, child class object in parent
reference, array of parent pointers storing addresses of child classes
dynamic_cast
more
*/
Online C++ compiler to run C++ program online
/*
5) RTTI
virtual or pure virtual function
collect child class addresses in parent pointer, child class object in parent
reference, array of parent pointers storing addresses of child classes
dynamic_cast
more
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() const = 0;
};
class Rect :public Shape {
public:
void draw() const override {
cout << "Rect Drawing" << endl;
}
void Radius() {
cout << "Inside rect Extra()" << endl;
}
};
class Circle :public Shape {
public:
void draw() const override {
cout << "Circle Drawing" << endl;
}
void Dim() {
cout << "Inside Dim Extra()" << endl;
}
};
class Square :public Shape {};
int main() {
Shape* s[2];
s[0] = new Rect();
s[1] = new Circle();
Draw()
for (int i = 0;i < 2;i++) {
s[i]->draw();
cout << endl;
if (Rect* r = dynamic_cast<Rect*>(s[i])) {
r->Radius();
}
else if (Circle* c = dynamic_cast<Circle*>(s[i])) {
c->Dim();
}
cout << endl;
}
for (int i = 0; i < 1; ++i) {
delete s[i];
}
return 0;
}
Q6)
/*
class B
int y;
void disp2() { how will you access "x" here? }
class A
int x;
void disp1(){}
class A;
class B {
int y = 100;
public:
void disp2(const A&);
};
class A {
int x = 200;
public:
void disp1() {
cout << "Disp1" << endl;
}
friend class B;
};
Q7)
#include<iostream>
using namespace std;
/*
7) Given
class A
{
private:
int* ptr; allocate memory inside heap
int len;
void disp(){ incomplete }
};
while creating object of above class pass int array and its length
make sure when you call disp , it should display the whole array which was passed
while creating array
also make sure you can perform following operations on the objects of class A
cout<<ref1[1]; ref1 is assumed to be the object of A
ref1[0]=1000;
*/
#include <iostream>
using namespace std;
class A {
private:
int* ptr;
int len;
public:
A(int arr[], int length) {
len = length;
ptr = new int[len];
for (int i = 0; i < len; ++i) {
ptr[i] = arr[i];
}
}
void disp() {
for (int i = 0; i < len; ++i) {
cout << ptr[i] << " ";
}
cout << endl;
}
~A() {
delete[] ptr;
}
};
int main() {
int arr[] = { 10,20,30 };
A ref(arr,3);
ref.disp();
cout << "ref[1] = " << ref[1] << endl;
ref[0] = 1000;
cout << ref[0] << endl;