0% found this document useful (0 votes)
0 views7 pages

Assignment Prac

The document contains multiple C++ programming assignments focusing on class design, memory management, and operator overloading. Key topics include constructors, destructors, copy constructors, assignment operators, and dynamic memory allocation. It also covers concepts like RTTI, friend classes, and operator overloading for array-like access in custom classes.

Uploaded by

patilniraj277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views7 pages

Assignment Prac

The document contains multiple C++ programming assignments focusing on class design, memory management, and operator overloading. Key topics include constructors, destructors, copy constructors, assignment operators, and dynamic memory allocation. It also covers concepts like RTTI, friend classes, and operator overloading for array-like access in custom classes.

Uploaded by

patilniraj277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

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);

str2 = new char[strlen(n.str2) + 1];


strcpy_s(str2, strlen(n.str2) + 1, n.str2);

first& operator=(const first& n) {


str1 = new char[strlen(n.str1) + 1];
strcpy_s(str1, strlen(n.str1) + 1, n.str1);
str2 = new char[strlen(n.str2) + 1];
strcpy_s(str2, strlen(n.str2) + 1, n.str2);
return *this;
}

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);

Address = new char[strlen(ref.Address) + 1];


strcpy_s(Address, strlen(ref.Address) + 1, ref.Address);
}
return *this;
}

void input() {
cout << "\nEnter Voter ID: ";
cin >> id;
cin.ignore();

char tempName[100], tempAddress[200];

cout << "Enter Name: ";


cin.getline(tempName, 100);

cout << "Enter Address: ";


cin.getline(tempAddress, 200);

Name = new char[strlen(tempName) + 1];


strcpy_s(Name, strlen(tempName) + 1, tempName);

Address = new char[strlen(tempAddress) + 1];


strcpy_s(Address, strlen(tempAddress) + 1, tempAddress);
}

void show() const {


cout << "Voter ID: " << id << endl;
cout << "Name: " << Name << endl;
cout << "Address: " << Address << endl;
}

~Voter() {
delete[] Name;
delete[] Address;
cout << "Destructor called for ID: " << id << endl;
}
};
int main(){
int n;
cout << "How many voters? ";
cin >> n;
cin.ignore();

Voter* Voters = new Voter[n];

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


cout << "\nEnter details for voter " << i + 1 << ":";
Voters[i].input();
}

cout << "\nVoter Details:\n";


for (int i = 0; i < n; ++i) {
Voters[i].show();
}

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(){}

do not change the order of above classes


*/
#include<iostream>
using namespace std;

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;
};

void B::disp2(const A& a) {


cout << a.x << endl;
}
int main() {
A a;
B b;
b.disp2(a);
return 0;
}

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];
}
}

int& operator[](int index) {


if (index >= 0 && index < len)
return ptr[index];
else {
cout << "Index out of bounds.\n";
return ptr[0];
}
}

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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy