Files
ooplab/lab5/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.1 KiB

Imagine a publishing company that markets both book and audiocassette versions of its works. Create a class publication that stores the title (a string) and price (type float) of a publication. From this class derive two classes: book, which adds a page count (type int), and tape, which adds a playing time in minutes (type float). Each of these three classes should have a getdata() function to get its data from the user at the keyboard, and a putdata() function to display its data. (Simple inheritance & method overriding)

#include <iostream>
using namespace std;

class Work {
private:
  char title[50];
  uint price;

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

void Work::getData() {
  cout << "Title of publication: ";
  cin >> title;
  cout << "Price of publication: ";
  cin >> price;
}

void Work::putData() {
  cout << "Title of publication: " << title << endl
       << "Price of publication: " << price << endl;
}

class Book : public Work {
private:
  uint pageCount;

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

void Book::getData() {
  Work::getData();
  cout << "Number of pages: ";
  cin >> pageCount;
}

void Book::putData() {
  Work::putData();
  cout << "Number of pages: " << pageCount << endl;
}

class Tape : public Work {
private:
  uint lengthMin;

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

void Tape::getData() {
  Work::getData();
  cout << "Length in minutes: ";
  cin >> lengthMin;
}

void Tape::putData() {
  Work::putData();
  cout << "Length in minutes: " << lengthMin << endl;
}

int main() {
  Tape t;
  Book b;
  t.getData();
  t.putData();
  b.getData();
  b.putData();
}
Title of publication: alo
Price of publication: 4
Length in minutes: 4
Title of publication: alo
Price of publication: 4
Length in minutes: 4
Title of publication: cope
Price of publication: 44
Number of pages: 444
Title of publication: cope
Price of publication: 44
Number of pages: 444

C++ program to read and print employee information using multiple inheritance. The program has following classes:

basicInfo

  • Data: name[char,30],empId[int], gender[char]
  • Functions: getData(), putData();

deptInfo

  • Data: deptName[char,30], assignWork[char,30], timeToComplete(int)
  • Functions: getData(), putData();

employeeInfo

  • Data: salary[int], age[int]
  • Functions: getData(), putData();
#include <iostream>
using namespace std;

class BasicInfo {
private:
  char name[30];
  int empId;
  char gender;

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

void BasicInfo::getData() {
  cout << "Name: ";
  cin >> name;
  cout << "Employee ID: ";
  cin >> empId;
  cout << "Gender: ";
  cin >> gender;
}

void BasicInfo::putData() {
  cout << "Name: " << name << endl
       << "Employee ID: " << empId << endl
       << "Gender: " << gender << endl;
}

class DeptInfo : public BasicInfo {
private:
  char deptName[30];
  char assignWork[30];
  int timeToComplete;

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

void DeptInfo::getData() {
  BasicInfo::getData();
  cout << "Name of Department: ";
  cin >> deptName;
  cout << "Assigned Work: ";
  cin >> assignWork;
  cout << "Time to complete: ";
  cin >> timeToComplete;
}

void DeptInfo::putData() {
  BasicInfo::putData();
  cout << "Name of Department: " << deptName << endl
       << "Assigned Work: " << assignWork << endl
       << "Time to complete: " << timeToComplete << endl;
}

class EmployeeInfo : public DeptInfo {
private:
  int salary;
  int age;

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

void EmployeeInfo::getData() {
  DeptInfo::getData();
  cout << "Salary of employee: ";
  cin >> salary;
  cout << "Age of employee: ";
  cin >> age;
}

void EmployeeInfo::putData() {
  DeptInfo::putData();
  cout << "Salary of employee: " << salary << endl
       << "Age of employee: " << age << endl;
}

int main() {
  EmployeeInfo e;
  e.getData();
  e.putData();

  return 0;
}
Name: QuantaviousQuandaleIII
Employee ID: 4
Gender: f
Name of Department: BasedDepartment
Assigned Work: DiscordModeration       
Time to complete: 4
Salary of employee: 444444
Age of employee: 44
Name: QuantaviousQuandaleIII
Employee ID: 4
Gender: f
Name of Department: BasedDepartment
Assigned Work: DiscordModeration
Time to complete: 4
Salary of employee: 444444
Age of employee: 44

Design three classes STUDENT ,EXAM and RESULT. The STUDENT class has data members such as rollno, name. create a class EXAM by inheriting the STUDENT class. The EXAM class adds data members representing the marks scored in six subjects. Derive the RESULT from the EXAM class and has its own data members such as total marks. Write a program to model this relationship.

#include <iostream>
using namespace std;

class Student {
private:
  char name[30];
  int enrollment;

public:
  void getData();
};

void Student::getData() {
  cout << "Name: ";
  cin >> name;
  cout << "Enrollment: ";
  cin >> enrollment;
}

class Exam : public Student {
protected:
  const static uint NSUBS = 6;
  uint marks[NSUBS];

public:
  void getData();
};

void Exam::getData() {
  uint i;

  Student::getData();

  cout << "Enter marks for " << endl;

  for (i = 0; i < NSUBS; i++) {
    cout << "Subject " << i + 1 << ": ";
    cin >> marks[i];
  }
}

class Result : private Exam {
private:
  uint totalMarks;

public:
  void getData();
  uint getTotal();
};

void Result::getData() {
  uint i;

  Exam::getData();
  this->totalMarks = 0;

  for (i = 0; i < Exam::NSUBS; i++)
    this->totalMarks += Exam::marks[i];
}

uint Result::getTotal() { return this->totalMarks; }

int main() {
  Result r;
  r.getData();
  cout << "Total Marks: " << r.getTotal();
  return 0;
}
Name: RetardSingh
Enrollment: 4
Enter marks for 
Subject 1: 44
Subject 2: 4
Subject 3: 30
Subject 4: 99
Subject 5: 26
Subject 6: 84