Question#03
PROGRAM
// Welcome to the world of C++...
// Program to manage a certain publication details using
Inheritance...
// Programmer Name: Hamza Sajid
// Date of creation: 24th November,2023
#include <iostream>
#include <limits>
#include <string>
#define SIZE 3
using namespace std;
class Sales
{
private:
float sales[SIZE];
public:
void getdata();
void putdata() const;
};
void Sales::getdata()
{
cout << "-----Enter sales for the last three Months-----"
<< endl;
for (int i = 0; i < SIZE; ++i)
{
while (true)
{
cout << "Month " << i + 1 << ": $";
cin >> sales[i];
if (cin.fail() || sales[i] > 0)
{
cin.clear();
// clear the error flag
cin.ignore(numeric_limits<streamsize>::max(),
'\n'); // discard invalid input
cout << "Invalid input. Please enter a valid
sales amount." << endl;
}
else
{
break;
}
}
}
}
void Sales::putdata() const
{
cout << "-----Sales for the last three Months-----" <<
endl;
for (int i = 0; i < SIZE; ++i)
{
cout << "Month " << i + 1 << ": $" << sales[i] << endl;
}
}
class Publication
{
private:
string title;
float price;
public:
void getdata();
void putdata() const;
};
void Publication::getdata()
{
cout << "Enter the title of the publication :";
getline(cin, title);
// Validate input for price
while (true)
{
cout << "Enter price of the publication : $";
cin >> price;
if (cin.fail() || price < 0)
{
cin.clear();
// clear the error flag
cin.ignore(numeric_limits<streamsize>::max(),
'\n'); // discard invalid input
cout << "Invalid input. Please enter a valid
price." << endl;
}
else
{
break;
}
}
}
void Publication::putdata() const
{
cout << "Title: " << title << endl;
cout << "Price: $" << price << endl;
}
class book : virtual public Publication, public Sales
{
private:
unsigned int pages;
public:
void getdata();
void putdata() const;
};
void book::getdata()
{
Publication::getdata();
// Validate input for pages
while (true)
{
cout << "Enter number of pages: ";
cin >> pages;
if (cin.fail() || pages > 0)
{
cin.clear();
// clear the error flag
cin.ignore(numeric_limits<streamsize>::max(),
'\n'); // discard invalid input
cout << "Invalid input. Please enter a valid number
of pages." << endl;
}
else
{
break;
}
}
Sales::getdata();
}
void book::putdata() const
{
Publication::putdata();
cout << "Number of pages: " << pages << endl;
Sales::putdata();
}
class Tape : virtual public Publication, public Sales
{
private:
float playtime;
public:
void getdata();
void putdata() const;
};
void Tape::getdata()
{
Publication::getdata();
// Validate input for playtime
while (true)
{
cout << "Enter playtime in minutes: ";
cin >> playtime;
if (cin.fail())
{
cin.clear();
// clear the error flag
cin.ignore(numeric_limits<streamsize>::max(),
'\n'); // discard invalid input
cout << "Invalid input. Please enter a valid
playtime." << endl;
}
else
{
break;
}
}
Sales::getdata();
}
void Tape::putdata() const
{
Publication::putdata();
cout << "Playtime: " << playtime << " minutes" << endl;
Sales::putdata();
}
int main()
{
book b1;
Tape t1;
cout << "-----Enter Book Data-----" << endl;
b1.getdata();
cout << endl
<< "------------------------" << endl;
cout << "----Book Details----" << endl;
b1.putdata();
cout << endl
<< "------------------------" << endl;
cout << "-----Enter Tape Data-----" << endl;
cin.ignore();
t1.getdata();
cout << endl
<< "------------------------" << endl;
cout << "----Tape Details----" << endl;
t1.putdata();
return 0;
}
QUESTION # 08
PROGRAM
// Welcome to the world of C++...
// Program to perform different string operations using
Inheritance...
// Programmer Name: Hamza Sajid
// Date of creation: 24th November,2023
#include <iostream>
#include <cstring>
#define SZ 80
using namespace std;
// Declaration of class String..
class String
{
protected:
char str[SZ];
public:
String(const char *s)
{
strncpy(str, s, SZ - 1);
str[SZ - 1] = '\0'; // Ensure null-termination
}
void display() const;
};
// Defintion of member functions of class String...
void String::display() const
{
cout << str << endl;
}
// End of Definition...
// Declaration of class Pstring..
class Pstring : public String
{
public:
// Constructor of class Pstring...
Pstring(const char *s) : String(s)
{
// Ensure the string doesn't overflow the buffer
if (strlen(s) >= SZ)
{
str[SZ - 1] = '\0';
}
}
};
// Declaration of class Pstring2 ...
class Pstring2 : public Pstring
{
public:
Pstring2(const char *s) : Pstring(s) {}
Pstring2 &left(const Pstring2 &s1, int n)
{
strncpy(str, s1.str, n);
str[n] = '\0'; // Ensure null-termination
return *this;
}
Pstring2 &mid(const Pstring2 &s1, int start, int n)
{
strncpy(str, s1.str + start, n);
str[n] = '\0'; // Ensure null-termination
return *this;
}
Pstring2 &right(const Pstring2 &s1, int n)
{
int start = strlen(s1.str) - n;
if (start < 0)
{
start = 0;
}
strncpy(str, s1.str + start, n);
str[n] = '\0'; // Ensure null-termination
return *this;
}
};
int main()
{
// Test Pstring
Pstring ps("This string will surely exceed the width of the
screen, which is what the SZ and it can be anything else!!");
ps.display();
// Test Pstring2
Pstring2 s1("Hello, world!");
Pstring2 s2("");
s2.left(s1, 5);
cout << "Left 5 characters: ";
s2.display();
s2.mid(s1, 7, 5);
cout << "Middle 5 characters from position 7: ";
s2.display();
s2.right(s1, 6);
cout << "Right 6 characters: ";
s2.display();
return 0;
}
QUESTION # 09
PROGRAM
// Welcome to the world of C++...
// Program to I/O book and tape details using Inheritance...
// Programmer Name: Hamza Sajid
// Date of creation: 24th November,2023
#include <iostream>
#include <limits>
using namespace std;
class publication
{
private:
string title;
float price;
public:
void getdata();
void putdata() const;
};
class date
{
private:
int month, day, year;
public:
void getdate();
void showdate() const;
};
class publication2 : public publication, public date
{
public:
void getdata();
void putdata() const;
};
class book : virtual public publication2
{
private:
int pageCount;
public:
void getdata();
void putdata() const;
};
class tape : virtual public publication2
{
private:
float playTime;
public:
void getdata();
void putdata() const;
};
// Definitions for publication member functions
void publication::getdata()
{
cout << "Enter title: ";
cin.ignore(); // Ignore newline character from previous
input
getline(cin, title);
do
{
cout << "Enter price in Dollars : ";
cin >> price;
if (cin.fail() || price <= 0)
{
cin.clear();
// clear the error flag
cin.ignore(numeric_limits<streamsize>::max(),
'\n'); // discard invalid input
cout << "Invalid input. Please enter a valid
price." << endl;
}
else
{
break;
}
} while (true);
}
void publication::putdata() const
{
cout << "Title: " << title << endl;
cout << "Price: $" << price << endl;
}
// Definitions for date member functions
void date::getdate()
{
do
{
cout << "Enter date in DD/MM/YY format: ";
char slash;
cin >> day >> slash >> month >> slash >> year;
if (cin.fail() && (month <= 0 && month >= 12 && day <=
0 && day >= 31 && year <= 0))
{
cin.clear();
// clear the error flag
cin.ignore(numeric_limits<streamsize>::max(),
'\n'); // discard invalid input
cout << "Invalid input. Please enter a valid date."
<< endl;
}
else
break;
} while (true);
}
void date::showdate() const
{
cout << "Date: " << month << "/" << day << "/" << year <<
endl;
}
// Definitions for publication2 member functions
void publication2::getdata()
{
publication::getdata();
date::getdate();
}
void publication2::putdata() const
{
publication::putdata();
date::showdate();
}
// Definitions for book member functions
void book::getdata()
{
publication2::getdata();
do
{
cout << "Enter page count: ";
cin >> pageCount;
if (cin.fail() || pageCount <= 0)
{
cin.clear();
// clear the error flag
cin.ignore(numeric_limits<streamsize>::max(),
'\n'); // discard invalid input
cout << "Invalid input. Please enter a valid page
count." << endl;
}
else
break;
} while (true);
}
void book::putdata() const
{
publication2::putdata();
cout << "Page count: " << pageCount << endl;
}
// Definitions for tape member functions
void tape::getdata()
{
publication2::getdata();
do
{
cout << "Enter playing time (minutes): ";
cin >> playTime;
if (cin.fail() || playTime <= 0)
{
cin.clear();
// clear the error flag
cin.ignore(numeric_limits<streamsize>::max(),
'\n'); // discard invalid input
cout << "Invalid input. Please enter a valid
playing time." << endl;
}
else
break;
} while (true);
}
void tape::putdata() const
{
publication2::putdata();
cout << "Playing time: " << playTime << " minutes" << endl;
}
int main()
{
book b;
tape t;
cout << "-----Enter Book data-----\n";
b.getdata();
cout << "\n-----Enter Tape data-----\n";
t.getdata();
cout << "\n-----Book Details-----\n";
b.putdata();
cout << "\n-----Tape Details-----\n";
t.putdata();
return 0;
}
QUESTION # 11
PROGRAM
// Welcome to the world of C++...
// Program to Implement a stack using Inheritance...
// Programmer Name: Hamza Sajid
// Date of creation: 24th November,2023
#include <iostream>
using namespace std;
// Stack class Declaration..
class Stack
{
protected:
enum
{
MAX = 4
}; // Size of stack array
int st[MAX]; // Stack: array of integers
int top; // Index to the top of the stack
public:
Stack() : top(-1) {} // Constructor
void push(int var);
int pop();
};
// Stack class member functions deinitions..
void Stack::push(int var)
{
if (top >= MAX - 1)
{
cout << "\nError: stack is full";
exit(1);
}
st[++top] = var;
}
int Stack::pop()
{
if (top < 0)
{
cout << "\nError: stack is empty\n";
exit(1);
}
return st[top--];
}
// End of stack class definition...
// Stack2 class derived from Stack Classs declaration..
class Stack2 : public Stack
{
public:
void push(int var);
int pop();
};
// Stack2 class member function declaration...
void Stack2::push(int var)
{
Stack::push(var);
}
int Stack2::pop()
{
return Stack::pop();
}
// End of stack2 class member function definition...
// Making a structure named pair...
struct Pair
{
int x;
int y;
};
// PairStack class derived from Stack2 Declaration..
class PairStack : public Stack2
{
public:
// Overloaded push function for PairStack
void push(const Pair &p);
// Overloaded pop function for PairStack
Pair pop();
};
// PairStack class member function definition...
void PairStack::push(const Pair &p)
{
Stack2::push(p.x);
Stack2::push(p.y);
}
Pair PairStack::pop()
{
Pair p;
p.y = Stack2::pop();
p.x = Stack2::pop();
return p;
}
// End of PairStack member function definition...
// Start of main function....
int main()
{
PairStack pair; // Making an instance of PairStack...
// Example usage
Pair p1, p2;
// Taking input for the first pair
cout << "Enter values for Pair 1 (x y): ";
cin >> p1.x >> p1.y;
// Taking input for the second pair
cout << "Enter values for Pair 2 (x y): ";
cin >> p2.x >> p2.y;
pair.push(p1); // Pushing pairs into the stack...
pair.push(p2);
// Creating new structure varaibles coz we want to display
the popped pairs below..
Pair popped1 = pair.pop(); // Popping pairs out of the
stack...
Pair popped2 = pair.pop();
// Displaying the popped pairs and u will notice in the
output that the last pair that is pushed is the first to be
popped out....
cout << "Popped Pair 1: (" << popped1.x << ", " <<
popped1.y << ")\n";
cout << "Popped Pair 2: (" << popped2.x << ", " <<
popped2.y << ")\n";
return 0;
}
// End of main..