0% found this document useful (0 votes)
68 views28 pages

C++ Advanced Programming Concepts

The document outlines various C++ programming assignments focusing on advanced concepts such as deep copy, operator overloading, multiple inheritance, templates, polymorphism, exception handling, and more. It includes example code snippets for each assignment, demonstrating the implementation of these concepts in practical scenarios. Additionally, it features assignments related to the C++ STL, specifically using std::map for tasks like word frequency counting and student record management.

Uploaded by

G.satheesh Reddy
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)
68 views28 pages

C++ Advanced Programming Concepts

The document outlines various C++ programming assignments focusing on advanced concepts such as deep copy, operator overloading, multiple inheritance, templates, polymorphism, exception handling, and more. It includes example code snippets for each assignment, demonstrating the implementation of these concepts in practical scenarios. Additionally, it features assignments related to the C++ STL, specifically using std::map for tasks like word frequency counting and student record management.

Uploaded by

G.satheesh Reddy
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

[Link] deep copy using copy constructor and assignment operator. 2.

Program to overload new and delete operators for a class. 3. Implement multiple
inheritance and resolve ambiguity using scope resolution. 4. Create template
class for generic Vector with add and display methods. 5. Program to
demonstrate runtime polymorphism using virtual functions. 6. Implement
exception handling with custom exception class. 7. Program to overload
subscript [] operator for array class. 8. Write program to demonstrate diamond
problem and virtual inheritance. 9. Implement function template and class
template together. 10. Program to design class with static members and static
functions managing shared data. 11. Implement move constructor and move
assignment operator. 12. Program to overload () function call operator. 13.
Create base class pointer managing derived objects and show virtual destructor
effect. 14. Implement template-based sort function for different data types. 15.
Program to demonstrate const correctness in complex class design. 16.
Implement reference return from function and show its use. 17. Program to
overload type conversion operator. 18. Design class hierarchy and demonstrate
method overriding with base references. 19. Implement exception safe code
using try, catch, and finally-like behavior. 20. Create mini project: Student
Management using classes, templates, inheritance and exceptions.

1️⃣ Deep Copy (Copy Constructor + Assignment Operator)


#include <iostream>
using namespace std;

class Test {
int *p;
public:
Test(int x) {
p = new int(x);
}

// Copy Constructor
Test(const Test& t) {
p = new int(*t.p);
}

// Assignment Operator
Test& operator=(const Test& t) {
if(this != &t) {
delete p;
p = new int(*t.p);
}
return *this;
}

~Test() {
delete p;
}

void show() { cout << *p << endl; }


};

int main() {
Test t1(10);
Test t2 = t1;
[Link]();
}
📘 Explanation
• Allocates separate memory
• Prevents double free
• Mandatory for classes with dynamic memory

2️⃣ Overload new and delete


#include <iostream>
using namespace std;

class Test {
public:
void* operator new(size_t size) {
cout << "Custom new\n";
return malloc(size);
}
void operator delete(void* p) {
cout << "Custom delete\n";
free(p);
}
};

int main() {
Test* t = new Test();
delete t;
}
📘 Explanation
• Controls memory allocation
• Used in memory tracking / embedded systems

3️⃣ Multiple Inheritance + Ambiguity Resolution


#include <iostream>
using namespace std;

class A {
public:
void show() { cout << "A\n"; }
};

class B {
public:
void show() { cout << "B\n"; }
};

class C : public A, public B {};

int main() {
C obj;
obj.A::show();
obj.B::show();
}
📘 Explanation
• Ambiguity resolved using scope resolution

4️⃣ Template Class (Generic Vector)


#include <iostream>
using namespace std;
template <class T>
class Vector {
T arr[5];
int idx;
public:
Vector():idx(0){}
void add(T x) { arr[idx++] = x; }
void display() {
for(int i=0;i<idx;i++)
cout << arr[i] << " ";
}
};

int main() {
Vector<int> v;
[Link](10); [Link](20);
[Link]();
}
📘 Explanation
• Reusable container
• Type-safe

5️⃣ Runtime Polymorphism


#include <iostream>
using namespace std;

class Base {
public:
virtual void show() {
cout << "Base\n";
}
};

class Derived : public Base {


public:
void show() {
cout << "Derived\n";
}
};

int main() {
Base* b = new Derived();
b->show();
}
📘 Explanation
• Virtual function → runtime binding

6️⃣ Custom Exception Class


#include <iostream>
using namespace std;

class MyException {
public:
const char* msg() {
return "Custom Exception";
}
};

int main() {
try {
throw MyException();
}
catch(MyException e) {
cout << [Link]();
}
}
📘 Explanation
• User-defined exception
• Improves error clarity

7️⃣ Overload Subscript []


#include <iostream>
using namespace std;

class Array {
int arr[5];
public:
int& operator[](int i) {
return arr[i];
}
};
int main() {
Array a;
a[0] = 10;
cout << a[0];
}
📘 Explanation
• Enables array-like access
• Must return reference

8️⃣ Diamond Problem + Virtual Inheritance


#include <iostream>
using namespace std;

class A {
public:
int x;
};

class B : virtual public A {};


class C : virtual public A {};
class D : public B, public C {};

int main() {
D obj;
obj.x = 10;
cout << obj.x;
}
📘 Explanation
• Virtual inheritance avoids duplicate base

9️⃣ Function Template + Class Template


#include <iostream>
using namespace std;

template <class T>


T add(T a, T b) {
return a + b;
}

template <class T>


class Test {
public:
void show(T x) {
cout << x << endl;
}
};

int main() {
Test<int> t;
[Link](add(10,20));
}
📘 Explanation
• Combines both templates
🔟 Static Members Managing Shared Data
#include <iostream>
using namespace std;

class Bank {
static int balance;
public:
static void deposit(int amt) {
balance += amt;
}
static void show() {
cout << balance;
}
};

int Bank::balance = 0;

int main() {
Bank::deposit(1000);
Bank::show();
}
📘 Explanation
• Static members → shared resource

1️⃣1️⃣ Move Constructor & Move Assignment


#include <iostream>
using namespace std;

class Test {
int *p;
public:
Test(int x) {
p = new int(x);
}

Test(Test&& t) {
p = t.p;
t.p = nullptr;
}

Test& operator=(Test&& t) {
if(this != &t) {
delete p;
p = t.p;
t.p = nullptr;
}
return *this;
}

~Test() { delete p; }
};
📘 Explanation
• Transfers ownership
• Avoids deep copy

1️⃣2️⃣ Overload Function Call Operator ()


#include <iostream>
using namespace std;

class Fun {
public:
void operator()(int x) {
cout << "Called with " << x;
}
};

int main() {
Fun f;
f(10);
}
📘 Explanation
• Object behaves like function

1️⃣3️⃣ Virtual Destructor Effect


#include <iostream>
using namespace std;

class Base {
public:
virtual ~Base() {
cout << "Base\n";
}
};

class Derived : public Base {


public:
~Derived() {
cout << "Derived\n";
}
};

int main() {
Base* b = new Derived();
delete b;
}
📘 Explanation
• Derived destructor called correctly

1️⃣4️⃣ Template-Based Sort


#include <iostream>
using namespace std;

template <class T>


void sortArr(T arr[], int n) {
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(arr[i] > arr[j])
swap(arr[i], arr[j]);
}

int main() {
int a[] = {3,1,2};
sortArr(a,3);
for(int x:a) cout<<x<<" ";
}
📘 Explanation
• Works for any comparable type

1️⃣5️⃣ Const Correctness


#include <iostream>
using namespace std;

class Test {
int x;
public:
Test(int a):x(a){}
int get() const {
return x;
}
};
📘 Explanation
• Const functions protect data

1️⃣6️⃣ Reference Return


#include <iostream>
using namespace std;

int& fun(int &x) {


return x;
}

int main() {
int a = 10;
fun(a) = 20;
cout << a;
}
📘 Explanation
• Enables lvalue assignment

1️⃣7️⃣ Type Conversion Operator


#include <iostream>
using namespace std;

class Test {
int x;
public:
Test(int a):x(a){}
operator int() {
return x;
}
};
int main() {
Test t(10);
int a = t;
cout << a;
}
📘 Explanation
• Implicit conversion

1️⃣8️⃣ Overriding with Base Reference


#include <iostream>
using namespace std;

class Base {
public:
virtual void show() {
cout << "Base\n";
}
};

class Derived : public Base {


public:
void show() {
cout << "Derived\n";
}
};
int main() {
Derived d;
Base& b = d;
[Link]();
}
📘 Explanation
• Base reference → derived method

1️⃣9️⃣ Exception Safe Code (Finally-like)


#include <iostream>
using namespace std;

class Resource {
public:
Resource() { cout << "Allocated\n"; }
~Resource() { cout << "Released\n"; }
};

int main() {
try {
Resource r;
throw 1;
}
catch(...) {
cout << "Exception\n";
}
}
📘 Explanation
• Destructor acts like finally

2️⃣0️⃣ Mini Project: Student Management (Simplified)


#include <iostream>
using namespace std;

template <class T>


class Student {
T id;
string name;
public:
Student(T i, string n):id(i),name(n){}
void show() {
cout << id << " " << name << endl;
}
};

int main() {
Student<int> s1(1,"Ravi");
Student<int> s2(2,"Anil");
[Link]();
[Link]();
}
📘 Explanation
• Uses class, template, constructor
• Base for full project
Below is a structured, exam-friendly answer with short explanation + full
code for Assignments 1️–1️0️.

📘 C++ STL – std::map (Practice Programs)

🔹 Assignment 1️ — Word Frequency Counter (Easy)

🔍 Concept
map<string, int> automatically stores words in sorted order and counts
frequency.
✅ Program
#include <iostream>
#include <map>
#include <sstream>
using namespace std;

int main() {
map<string, int> freq;
string line, word;

getline(cin, line);
stringstream ss(line);

while (ss >> word)


freq[word]++;

for (auto &p : freq)


cout << [Link] << " : " << [Link] << endl;
}

🔹 Assignment 2️ — Character Frequency (Easy)

🔍 Concept
Each character is a key, frequency is the value.
#include <iostream>
#include <map>
using namespace std;

int main() {
map<char, int> freq;
string s;
cin >> s;

for (char c : s)
freq[c]++;

for (auto &p : freq)


cout << [Link] << " : " << [Link] << endl;
}

🔹 Assignment 3️ — Student Marks Record (Easy)


🔍 Concept
Map keeps student names sorted alphabetically.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> marks;

marks["Alice"] = 90;
marks["Bob"] = 70;
marks["John"] = 80;

for (auto &p : marks)


cout << [Link] << " : " << [Link] << endl;
}

🔹 Assignment 4️ — Phonebook Using Map (Easy)


#include <iostream>
#include <map>
using namespace std;

int main() {
map<string, string> phone;
int choice;
string name, number;

while (1) {
cout << "\[Link] [Link] [Link] [Link] [Link]\n";
cin >> choice;

if (choice == 1) {
cin >> name >> number;
phone[name] = number;
}
else if (choice == 2) {
cin >> name;
if ([Link](name))
cout << phone[name];
else
cout << "Not found";
}
else if (choice == 3) {
cin >> name;
[Link](name);
}
else if (choice == 4) {
for (auto &p : phone)
cout << [Link] << " : " << [Link] << endl;
}
else break;
}
}

🔹 Assignment 5️ — Count Distinct Integers (Medium)


#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, int> freq;
int x;

while (cin >> x)


freq[x]++;

for (auto &p : freq)


cout << [Link] << " : " << [Link] << endl;
}

🔹 Assignment 6️ — Group Cities by Country (Medium)


🔍 Concept
Map of country → vector of cities
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
map<string, vector<string>> data;

data["India"].push_back("Delhi");
data["India"].push_back("Mumbai");
data["France"].push_back("Paris");
data["France"].push_back("Lyon");
for (auto &p : data) {
cout << [Link] << " -> ";
for (auto &city : [Link])
cout << city << " ";
cout << endl;
}
}

🔹 Assignment 7️ — Sort by Frequency (Medium)


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
map<int, int> freq;
vector<pair<int, int>> v;
int x;

while (cin >> x)


freq[x]++;

for (auto &p : freq)


v.push_back({[Link], [Link]});

sort([Link](), [Link]());
for (auto &p : v)
cout << [Link] << " : " << [Link] << endl;
}

🔹 Assignment 8️ — First Non-Repeating Character (Medium)


#include <iostream>
#include <map>
using namespace std;

int main() {
string s;
cin >> s;

map<char, int> freq;


for (char c : s)
freq[c]++;

for (char c : s) {
if (freq[c] == 1) {
cout << c;
break;
}
}
}

🔹 Assignment 9️ — Student Ranking System (Hard)


🔍 Concept
Sort by marks (descending) using greater<int>.
#include <iostream>
#include <map>
using namespace std;

int main() {
map<int, string, greater<int>> rank;

rank[90] = "Alice";
rank[80] = "John";
rank[70] = "Bob";

for (auto &p : rank)


cout << [Link] << " : " << [Link] << endl;
}

🔹 Assignment 1️0️ — Dictionary / Thesaurus App (Hard)


#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
map<string, vector<string>> dict;
int choice;
string word, meaning;
while (1) {
cout << "\[Link] Word [Link] Meaning [Link] [Link] [Link]\n";
cin >> choice;

if (choice == 1) {
cin >> word;
dict[word];
}
else if (choice == 2) {
cin >> word >> meaning;
dict[word].push_back(meaning);
}
else if (choice == 3) {
cin >> word;
for (auto &m : dict[word])
cout << m << " ";
}
else if (choice == 4) {
for (auto &p : dict) {
cout << [Link] << " -> ";
for (auto &m : [Link])
cout << m << " ";
cout << endl;
}
}
else break;
}
}

You might also like