Files
ooplab/lab6/file.org
Amneesh Singh ad185c994e labs 1-6: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-24 14:03:08 +05:30

6.0 KiB

Create class first with data members book no, book name and member function getdata and putdata. Create a class second with data members author name ,publisher and members getdata and showdata. Derive a class third from first and second with data member no of pages and year of publication. Display all these information using an array of objects of third class.

#include <iostream>
using namespace std;

class Book {
private:
  char name[50];
  uint id;

public:
  void getData();
  void putData();
};

void Book::getData() {
  cout << "Name of book: ";
  cin >> name;
  cout << "ID of book: ";
  cin >> id;
}

void Book::putData() {
  cout << "Name of book: " << name << endl << "ID of book: " << id << endl;
}

class Authorities : public Book {
private:
  char author[50];
  char publisher[50];

public:
  void getData();
  void putData();
};

void Authorities::getData() {
  cout << "Name of author: ";
  cin >> author;
  cout << "Name of publisher: ";
  cin >> publisher;
}

void Authorities::putData() {
  cout << "Name of author: " << author << endl
       << "Name of publisher: " << publisher << endl;
}

class Publication : public Authorities {
private:
  uint pageCount;
  uint year;

public:
  void getData();
  void putData();
};

void Publication::getData() {
  cout << "Number of pages: ";
  cin >> pageCount;
  cout << "Year (YYYY): ";
  cin >> year;
}

void Publication::putData() {
  cout << "Number of pages: " << pageCount << endl << "Year: " << year << endl;
}

int main() {
  Publication p[3];

  for (auto &x: p) {
    x.Book::getData();
    x.Authorities::getData();
    x.getData();
  }

  for (auto &x: p) {
    x.Book::putData();
    x.Authorities::putData();
    x.putData();
  }
}
ID of book: 4
Name of author: a.
Name of publisher: a.
Number of pages: 4
Year (YYYY): 4044
Name of book: sahi
ID of book: 5
Name of author: baat
Name of publisher: hai
Number of pages: 555555
Year (YYYY): 192
Name of book: how
ID of book: 349234
Name of author: ok
Name of publisher: test
Number of pages: 99991293
Year (YYYY): 2022
Name of book: Allu
ID of book: 4
Name of author: a.
Name of publisher: a.
Number of pages: 4
Year: 4044
Name of book: sahi
ID of book: 5
Name of author: baat
Name of publisher: hai
Number of pages: 555555
Year: 192
Name of book: how
ID of book: 349234
Name of author: ok
Name of publisher: test
Number of pages: 99991293
Year: 2022

Create a base class called SHAPE. Use this class to store two double type values. Derive two specific classes called TRIANGLE and RECTANGLE from the base class. Add to the base class, a member function getdata to initialize base class data members and another member function display to compute and display the area of figures. Make display a virtual function and redefine this function in the derived classes to suit their requirements. Using these three classes design a program that will accept driven of a TRIANGLE or RECTANGLE interactively and display the area.

#include <iostream>
#include <math.h>
using namespace std;

class Shape {
  int stub;

public:
  virtual void getData() { cout << "stub"; }

  virtual double area() {
    cout << "stub";
    return 0;
  }
};

class Triangle : public Shape {
private:
  double sides[3];

public:
  void getData();
  double area();
};

void Triangle::getData() {
  cout << "Enter 3 integers for the sides of triangle: \n";

  for (int i = 0; i < 3; i++)
    cin >> sides[i];
}

double Triangle::area() {
  double s = (sides[0] + sides[1] + sides[2]) / 2;

  return sqrt(s * (s - sides[0]) * (s - sides[1]) * (s - sides[2]));
}

class Rectangle : public Shape {
private:
  double l;
  double b;

public:
  void getData();
  double area();
};

void Rectangle::getData() {
  cout << "Enter length of rectangle: ";
  cin >> l;
  cout << "Enter breadh of rectangle: ";
  cin >> b;
}

double Rectangle::area() { return l * b; }

int main() {
  Shape *s;
  Triangle t;
  Rectangle r;

  s = &t;

  s->getData();
  cout << "Area of the Shape: " << s->area() << endl;

  s = &r;

  s->getData();
  cout << "Area of the Shape: " << s->area() << endl;

}
Enter 3 integers for the sides of triangle: 
4
4
4
Area of the Shape: 6.9282
Enter length of rectangle: 4
Enter breadh of rectangle: 4
Area of the Shape: 16

Create a base class basic_info with data members name ,roll no, gender and two member functions getdata and display. Derive a class physical_fit from basic_info which has data members height and weight and member functions getdata and display. Display all the information using object of derived class.

#include <iostream>
using namespace std;

class BasicInfo {
private:
  char name[30];
  uint enrollment;
  char gender;

public:
  void getData();
  void putData();
};

void BasicInfo::getData() {
  cout << "Name: ";
  cin >> name;
  cout << "Enrollment: ";
  cin >> enrollment;
  cout << "Gender: ";
  cin >> gender;
}
void BasicInfo::putData() {
  cout << "Name: " << name << endl
       << "Enrollment: " << enrollment << endl
       << "Gender: " << gender << endl;
}

class PhysicalFit : public BasicInfo {
  uint height;
  uint weight;

public:
  void getData();
  void putData();
};

void PhysicalFit::getData() {
  cout << "Weight (in kg): ";
  cin >> weight;
  cout << "Height (in cm): ";
  cin >> height;
}

void PhysicalFit::putData() {
  cout << "Weight (in kg): " << weight << endl
       << "Height (in cm): " << height << endl;
}

int main() {
  PhysicalFit x;
  x.BasicInfo::getData();
  x.getData();

  x.BasicInfo::putData();
  x.putData();

  return 0;
}
Name: acha
Enrollment: 4
Gender: f
Weight (in kg): 999
Height (in cm): 2
Name: acha
Enrollment: 4
Gender: f
Weight (in kg): 999
Height (in cm): 2