301
lab5/file.org
Normal file
301
lab5/file.org
Normal file
@@ -0,0 +1,301 @@
|
||||
* 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)
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 13.cpp :results output :wrap src text
|
||||
#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();
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+begin_src text
|
||||
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
|
||||
#+end_src
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* 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();
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 14.cpp :results output :wrap src text
|
||||
#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;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+begin_src
|
||||
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
|
||||
#+end_src
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* 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.
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 15.cpp :results output :wrap src text
|
||||
#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;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+begin_src
|
||||
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
|
||||
#+end_src
|
Reference in New Issue
Block a user