Home
OOP
Imagine a publishing company which does marketing for book and audio cassette versions. Create a class publication that stores the title (a string) and price (type float) of publications. 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). Write a program that instantiates the book and tape class, allows user to enter data and displays the data members. If an exception is caught, replace all the data member values with zero values
1 minute read
Object Oriented Programming Assignment 3
Imagine a publishing company which does marketing for book and audio cassette versions. Create a class publication that stores the title (a string) and price (type float) of publications. 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).

Write a program that instantiates the book and tape class, allows user to enter data and displays the data members. If an exception is caught, replace all the data member values with zero values



Code :


#include<iostream>
#program by Narendra Dwivedi Intel 20U520
#include<string>
using namespace std;
//base class publication
class publication
{
private:
string title;
float prices;
public:
publication()
 {
title="";
prices=0.0;
}
void get_data()
{
cout<<"\nEnter Title :";
cin.ignore();//clear input buffer
getline(cin,title);
cout<<"\nEnter Price : ";
cin>>prices;
}
void put_data()
{
cout<<"\n ________________________________ \n";
cout<<"\n Information : " <<endl;
cout<<"\n Title :"<<title;
cout<<"\n Price :"<<prices;
}
};
class book: public publication
{
private:
int pages;
public:
book(){
pages=0;
}
void get_data()
{
publication::get_data();
cout<<endl;
cout<<"Enter Page Count : \n";
cin>>pages;
}
void put_data()
{
publication::put_data();
try{
if(pages<0)
 throw pages;}
 catch(int f)
 {
 cout<<"\n error: pages not valid :"<<f;
 pages=0;
 }
cout<<"\n Pages Are :"<<pages;
}
};
class tape: public publication
{
private:
float playtime;
public:
tape()
{
playtime=0.0;
}
void get_data()
{
publication::get_data();
cout<<"Enter Play Time Of Cassette \n";
cin>>playtime;
}
void put_data()
{
publication::put_data();
try
{
if(playtime<0.0)
throw playtime;
}
catch(float r)
{
cout<<"\n Error: Invalid Playtime : "<<playtime;
playtime=0.0;
}
cout<<"\n Playtime is : "<<playtime;
}
};
int main()//main program
{
book b[10];// arrray of objects
tape t[10];
int choice=0,bookCount=0,tapeCount=0;
cout<<"-----------------------";
do
{
cout<<"\n 1. Add book ";
cout<<"\n 2. Add tape: ";
cout<<"\n 3. Display book ";
cout<<"\n 4. Display tape";
cout<<"\n 5. Exit:"<<endl;
cout<<"\n Enter Choice : ";
cin>>choice;
switch(choice)
{
case 1:
 {
 cout<<"\n--------------\n";
 cout<<"Add Book: \n";
 b[bookCount].get_data();
 bookCount++;
 break;
 }
case 2:
 {
 cout<<"\n--------------\n";
 cout<<"Add Tape: \n";
 t[tapeCount].get_data();
 tapeCount++;
 break;
 }
case 3:
 {
 cout<<"\n (books)";
 for(int j=0;j<bookCount;j++)
 {
 b[j].put_data();
 }
 break;
 }
case 4:
 {
 cout<<"\n (tape)";
 for(int j=0;j<tapeCount;j++)
 {
 t[j].put_data();
 }
 break;
 }
case 5:
 {
 cout<<"**********Program Exited
Successfully**********"<<endl;
exit(0);

 }
 default:
 {
 cout<<"\n Invalid";
 }
}
}
while(choice!=5);
return 0;
} 

C Language online compiler

Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!

Read inputs from stdin

OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.

#include <stdio.h>
int main()
{
    char name[50];
    printf("Enter name:");
    scanf("%s", name);
    printf("Hello %s \n" , name );
    return 0;
    
}

About C

C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.

Key features:

  • Structured Programming
  • Popular system programming language
  • UNIX, MySQL and Oracle are completely written in C.
  • Supports variety of platforms
  • Efficient and also handle low-level activities.
  • As fast as assembly language and hence used as system development language.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.

if(conditional-expression) {
   // code
} else {
   // code
}

You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to if-else-if ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

One dimentional Array:

data-type array-name[size];

Two dimensional array:

data-type array-name[size][size];

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

  1. Library Functions:

Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,

  1. User defined functions:

User defined functions are the ones which are written by the programmer based on the requirement.

How to declare a Function

return_type function_name(parameters);

How to call a Function

function_name (parameters)

How to define a Function

return_type function_name(parameters) {  
  //code
}