You are on page 1of 7

PPL Assignment 6

- U18CO072
- Hardik Upadhyay

1. Declare a class called author having author_name as private data member. Extend
author class to have two sub classes called book_publication &
paper_publication. Each of these classes have private member called title. Show
usage of dynamic method dispatch (dynamic polymorphism) to display book or
paper publications of a given author. Use command line arguments for inputting
data.
Code:
#include<bits/stdc++.h>
using namespace std;

class author{
protected:
string author_name;
public:
author(){}
author(string name):author_name(name){
// will be decided on runtime which get_title to call
}
virtual void get_title(){}
};

class book_publication:public author{


private:
string title;
public:
book_publication(string author_name, string
title_name):author(author_name),title(title_name){
}
virtual void get_title(){
cout << "The author " << author_name << " has published a book titled "
<< title << "\n";
}
};

class paper_publication:public author{


private:
string title;
public:
paper_publication(string author_name, string
title_name):author(author_name),title(title_name){
}
void get_title(){
cout << "The author " << author_name << " has published a paper titled "
<< title << "\n";
}
};

int main(int argc, char *argv[])


{
if(argc != 4){
cerr << "Insufficient number of arguments. format( type(0 for book,1 for
paper), author_name, title)";
return 1;
}
author *a;
string type = argv[1];
if(type == "0"){
book_publication b(argv[2], argv[3]);
a = &b;
a->get_title();
}
else if(type == "1"){
paper_publication p(argv[2], argv[3]);
a = &p;
a->get_title();
}
return 0;
}

2. Write a class named Rectangle to represent a rectangle. It contains the following


members:
Data: width (double) and height (double) that specify the width and height of the
rectangle.
Methods:
1. A no-arg constructor that creates a default rectangle.
2. A constructor that creates a rectangle with the specified width and height.
3. A method named getArea() that returns the area of this rectangle.
4. A method named getPerimeter() that returns the perimeter

Code:
#include<iostream>

using namespace std;

class Rectangle{
private:
double width, height;
public:
Rectangle(){
width = 0.0, height = 0.0;
cout << "Default constructor" << endl;
}
Rectangle(double w, double h):width(w),height(h){ cout <<
"Parameterized constructor with width " << width << " and height " << height << endl;}
double getArea(){ return width * height; }
double getPerimeter(){ return 2 * (width + height); }
};

int main()
{
Rectangle rect1;
cout << "Area of rectangle " << rect1.getArea() << endl;
cout << "Perimeter of rectangle " << rect1.getPerimeter() << endl;
Rectangle rect2(10,20);
cout << "Area of rectangle " << rect2.getArea() << endl;
cout << "Perimeter of rectangle " << rect2.getPerimeter() << endl;
return 0;
}
3. It is required to compute SPI (semester performance index) of n students of a class for
their registered subjects in a semester.
Assume that all students register for 6 subjects and each subject carry 5 credits. Also,
follow SVNIT convention and method for computation of SPI.
Declare a class called student having following data members:
id_no, grades_obtained and SPI.
Define constructor, display and calculate_spi methods. Define main to process data of n
students.
Code:
4. It is required to maintain and process the status of total 9 resources. The status value is
to be stored in an integer array of dimensions 3x3. The valid status of a resource can be
one of the following:
free: indicated by integer value 0
occupied: indicated by integer value 1
inaccessible: indicated by integer value 2
Declare a class called ResourcesStatus, having data member called statusRef, referring
to a two dimensional array (3x3) of integers to be used to refer to the above mentioned
status values. Define a member method called processStausCount that counts and
displays total number of free resources, total number of occupied resources and total
number of inaccessible resources. The exception to be raised and handled if total
number of occupied resources exceeds total number of free resources. The handler
marks status of all inaccessible resources as free. Accept initial status values from
command line arguments and initialize the array. Raise and handle user defined
exception if invalid status value given.

Code:
#include<bits/stdc++.h>
#include <exception>

using namespace std;

struct InsufficientMemory : public exception {


const char * what () const throw () {
return "Insufficient memory. Please free up more cells";
}
};

class ResourceStatus{
public:
vector<vector<int>> StatusRef;
ResourceStatus(){
StatusRef.resize(3);
for(auto &x: StatusRef) x.resize(3);
}

void setStatusRef(vector<vector<int>> sr) {


StatusRef = sr;
}

void processStatusCount(){
int n = StatusRef.size();
// assuming square matrix;
int f_c = 0, i_c = 0, o_c = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(StatusRef[i][j] == 0) f_c++;
if(StatusRef[i][j] == 1) o_c++;
if(StatusRef[i][j] == 2) i_c++;
}
}
cout << "Free Count: " << f_c << " Occupied count: " << o_c << "
Inaccessible count: " << i_c << endl;

if(o_c > f_c) {


// throw an exception
// handle by marking inaccessible resources as free
throw InsufficientMemory();
}
}
};

int main(int argc, char *argv[])


{
if(argc != 10) {
cerr << "Insufficient number of arguments" << endl;
exit(1);
}
vector<vector<int>> mat(3);
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
int x = argv[i*3 + j + 1][0] - '0';
mat[i].push_back(x);
}
}
ResourceStatus res;
res.StatusRef = mat;
try{
res.processStatusCount();
} catch(InsufficientMemory& e){
cout << e.what() << endl;
cout << "Freeing up inaccessible cells" << endl;
int n = res.StatusRef.size();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
res.StatusRef[i][j] = (res.StatusRef[i][j] == 2)? 0:
res.StatusRef[i][j];
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << res.StatusRef[i][j] << " ";
}
cout << endl;
}
}

return 0;
}

You might also like