0% found this document useful (0 votes)
49 views

Prog C Experiment

The program creates an employee class with protected data members for name and employee number. It derives two classes from employee - hourly_employee with private data for rate and hours, and salary_employee with private data for basic salary and allowance. Functions are defined to get input, calculate total salary, and display output for each derived class. Constructors and destructors are defined to print messages.

Uploaded by

Deepak Dhaimode
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Prog C Experiment

The program creates an employee class with protected data members for name and employee number. It derives two classes from employee - hourly_employee with private data for rate and hours, and salary_employee with private data for basic salary and allowance. Functions are defined to get input, calculate total salary, and display output for each derived class. Constructors and destructors are defined to print messages.

Uploaded by

Deepak Dhaimode
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

A book shop maintains the inventory of cout<<"For Book Shop Enter how many

books that one being sold at the shop. He list Records of BOOK u want to store"<<endl;
includes details such as authors, title, price, cin>>n;
publisher and stock position. Whenever a for(int i=0;i<n;i++)
customer wants a book, the sales person {
input the title and author and the system cout<<"Enter The Details of Book
searches the list and displays whether it is "<<i+1<<endl;
available or not. If it is not, an appropriate cout<<"Enter Author"<<endl;
message is displayed. If it is then the system cin>>authors[i];
displays the book details and requests for the cout<<"Enter Title"<<endl;
number of copies required. cin>>title[i];
cout<<"Enter Price"<<endl;
#include<iostream.h> cin>>price[i];
#include<conio.h> cout<<"Enter Publisher"<<endl;
#include<string.h> cin>>publisher[i];
class Book cout<<"Enter Stock Available"<<endl;
{ cin>>stock[i];
char authors[10][20]; }
char title[10][20]; }
int price[10]; void Book::display()
char publisher[10][20]; {
int stock[10]; cout<<"\nRecord of Books in Store
int n; are:"<<endl;
public: for(int i=0;i<n;i++)
void input(); {
void display(); cout<<"Details of book: "<<i+1<<endl;
void search(); cout<<"Author: "<<authors[i]<<endl;
}; cout<<"Title: "<<title[i]<<endl;
void Book::input() cout<<"Price: "<<price[i]<<endl;
{ cout<<"Publisher: "<<publisher[i]<<endl;
cout<<"Stock Available: "<<stock[i]<<endl;

Gurpreet Sir….. Page 1


} cout<<"Book Not Available in
} Store"<<endl;
void Book::search() }
{
char tit[20],aut[20]; void main()
int t=0; {
cout<<"\nWhat Book you want"<<endl; clrscr();
cout<<"Enter Title"<<endl; Book ob1;
cin>>tit; ob1.input();
cout<<"Enter Author"<<endl; ob1.display();
cin>>aut; ob1.search();
for(int i=0;i<n;i++) getch();
{ }
if((strcmp(title[i],tit)==0)&&(strcmp(author
s[i],aut)==0))
{
t=1;
cout<<"\nDetails of book: "<<endl;
cout<<"Author: "<<authors[i]<<endl;
cout<<"Title: "<<title[i]<<endl;
cout<<"Price: "<<price[i]<<endl;
cout<<"Publisher: "<<publisher[i]<<endl;
cout<<"Stock: "<<stock[i]<<endl;
cout<<"\nEnter how many books you
required of "<<tit<<endl;

break;
}
}
if(t==0)

Gurpreet Sir….. Page 2


Write a program which will show the order }
of execution of constructor, destructor, static
data member, static function and member void test:: showcode()
functions. {
cout<<"code="<<code<<endl;
#include<iostream.h> }
#include<conio.h> void test:: showcount()
class test {
{ cout<<"count="<<count<<endl;
int code; }
static int count; void main()
public: {
void get(); clrscr();
void showcode(); {
static void showcount(); test t1,t2,t3;
test(); t1.get(); t2.get(); t3.get();
~test(); t1.showcode(); test::showcount();
}; t2.showcode(); test::showcount();
test::test() t3.showcode(); test::showcount();
{ }
cout<<"Constructor.."<<endl; getch();
} }
test::~test()
{
cout<<"Destructor.."<<endl;
}
int test::count;
void test :: get()
{
count = count+1;
code=count;

Gurpreet Sir….. Page 3


Create class Distance having private data cin>>inches;
feet (type integer), inches (type float) and
//feet = f;
function getdist() and showdist() . Overload
+ operator to add two distance values and > //inches = i;
operator to compare them.
}

void Distance::showdist()
#include<iostream.h>
{
#include<conio.h>
cout<<"Distance in Feet:="<<feet<<"\t
class Distance
Inches:="<<inches<<"\n"<<endl;
{
}
int feet;
Distance Distance::operator +(Distance ob)
float inches;
{
public:
Distance d3;
void getdist();
d3.feet=feet+ob.feet;
void showdist();
d3.inches=inches+ob.inches;
Distance operator +(Distance ob);
if(d3.inches>=12)
Distance operator >(Distance ob);
{
};
d3.inches=d3.inches-12;
void Distance::getdist()
d3.feet=d3.feet+1;
{
}
cout<<"Enter Value of Feet"<<endl;
return d3;
cin>>feet;
}
cout<<"Enter Value of Inches:"<<endl;

Gurpreet Sir….. Page 4


Distance Distance::operator >(Distance ob) Distance d1,d2;

{ d1.getdist(); d1.showdist();

Distance d4; d2.getdist(); d2.showdist();

float a,b; Distance d3,d4;

a=feet*12+inches; d3=d1+d2;

b=ob.feet*12+ob.inches; cout<<"\nAfter Addition:="<<endl;

if(a>b) d3.showdist();

{ d4=d1>d2;

d4.feet=feet; cout<<"\nMaximum Distance from objects 1


and 2 is:"<<endl;
d4.inches=inches;
d4.showdist();
}
getch();
else
}
{

d4.feet=ob.feet;

d4.inches=ob.inches;

return d4;

void main()

clrscr();

Gurpreet Sir….. Page 5


Create a class called employee containing employee::employee()
protected data name (20 characters),
{
employee number (long integer). Also write
its constructor and destructor functions. cout<<"Constructor: Employee"<<endl;
Create two derived classes called hourly
}
_employee containing private data rate and
hours and salary_ employee containing basic employee::~employee()
salary and allowances as data members. The
{
class employee is inherited as public by
these derived classes. Write appropriate cout<<"Destructor: Employee"<<endl;
functions in each class to calculate total
}
salary of each employee and to display
name, number and total salary. class hourly_employee :public employee

{
#include<iostream.h>
int rate;
#include<conio.h>
int hours;
class employee
public:
{
void get();
protected:
void display();
char name[20];
};
long int emp_no;
class salary_employee : public employee
public:
{
employee();
int bsalary;
~employee();
int allowance;
};
public:

Gurpreet Sir….. Page 6


void get(); void salary_employee :: get()

void display(); {

}; cout<<"Enter Name:"<<endl;

void hourly_employee :: get() cin>>name;

{ cout<<"Enter Emp Number:"<<endl;

cout<<"Enter Name:"<<endl; cin>>emp_no;

cin>>name; cout<<"Enter Basic Salary:"<<endl;

cout<<"Enter Emp Number:"<<endl; cin>>bsalary;

cin>>emp_no; cout<<"Enter Total Allowance:"<<endl;

cout<<"Enter Remuneration Rate:"<<endl; cin>>allowance;

cin>>rate; }

cout<<"Total Hours:"<<endl; void salary_employee :: display()

cin>>hours; {

} int total = bsalary+allowance;

void hourly_employee :: display() cout<<"Name=: "<<name<<endl<<"Emp


Number=: "<<emp_no<<endl<<"Total
{
Salary=: "<<total<<"\n"<<endl;
int total = rate*hours;
}
cout<<"Name=: "<<name<<endl<<"Emp
void main()
Number=: "<<emp_no<<endl<<"Total
Salary=: "<<total<<"\n"<<endl; {

} clrscr();

Gurpreet Sir….. Page 7


salary_employee se;

se.get();

se.display();

hourly_employee he;

he.get();

he.display();

getch();

Gurpreet Sir….. Page 8


Create a class dimension containing three float area();
float type data and a constructor to accept };
values, also declare a pure virtual function Rectangle::Rectangle(float l , float b)
area() in it. Now create three derived classes {
rectangle, square and triangle, each length=l;
inheriting dimension as public. Define breadth=b;
corresponding constructors and redefine }
virtual function area() in each to give area of float Rectangle::area()
respective figure. A main() program should {
create suitable objects to implement this return (length*breadth);
inheritance. }

#include<iostream.h> class Square:public Dimension


#include<conio.h> {
//float side;
class Dimension public:
{ Square(float s);
protected: float area();
float length; };
float breadth; Square::Square(float s)
float side; {
public: side=s;
//Dimension(); }
virtual float area()=0; float Square::area()
}; {
class Rectangle:public Dimension return (side*side);
{ }
//float len;
//float breadth; class Triangle:public Dimension
public: {
Rectangle(float l, float b); //float base;

Gurpreet Sir….. Page 9


//float height; getch();
public: }
Triangle(float b, float h);
float area();
};
Triangle::Triangle(float b , float h)
{
length=b;
breadth=h;
}
float Triangle::area()
{
return (0.5*length*breadth);
}

void main()
{
clrscr();
Rectangle r1(5,10);
Square s1(6);
Triangle t1(2,4);
Dimension *ptr;
ptr=&r1;
float ar = ptr->area();
cout<<"Area of Rectangle:= "<<ar<<endl;
ptr=&s1;
ar = ptr->area();
cout<<"Area of Square:= "<<ar<<endl;
ptr=&t1;
ar = ptr->area();
cout<<"Area of Triangle:= "<<ar<<endl;

Gurpreet Sir….. Page 10


Create a class STRING that contains a {
character array as a data member. Overload STRING s3;
+ and == operators respectively to strcpy(s3.name,name);
concatenate and compare strings. strcat(s3.name,ob.name);
return s3;
#include<iostream.h> }
#include<conio.h> void STRING :: operator ==(STRING ob)
#include<string.h> {
class STRING int n=strcmp(name,ob.name);
{ if(n==0)
char name[20]; cout<<"Both String are Equal:"<<endl;
public: else if(n>0)
STRING(); cout<<"First String is Greater:"<<endl;
STRING(char ch[]); else
void show(); cout<<"Second String is Greater:"<<endl;
STRING operator +(STRING ob); }
void operator ==(STRING ob); void main()
}; {
STRING::STRING() clrscr();
{ STRING s1("FIRST");
} s1.show();
STRING :: STRING(char ch[]) STRING s2("SECOND");
{ s2.show();
strcpy(name,ch); STRING s3;
} cout<<"Operator + Overload:"<<endl;
void STRING::show() s3=s1+s2;
{ s3.show();
cout<<"string =:"<<name<<endl; cout<<"Operator == Overload:"<<endl;
} s1==s2;
STRING STRING :: operator +(STRING getch();
ob) }

Gurpreet Sir….. Page 11


Gurpreet Sir….. Page 12
Create two classes DM and DB respectively void get(int f, int i);
represent the distance in meters, centimeters void show();
and distance in feet, inches. Write a program friend void add (DM ob1, DB ob2);
that can read values for the class objects and };
add one object DM with another object of
DB. Use a friend function to carry out the void DM::get_dist(int m, int c)
addition operation. The object that stores the {
results may be a DM object or DB object meters = m;
depending on the units in which the results centimeters = c;
are required. The display should be in the }
format of feet and inches or meters and void DM::show_dist()
centimeters depending on the object on {
display. cout<<"Distance in meters = "<<meters<<"
centimeters= "<<centimeters<<endl;
#include<iostream.h> }
#include<conio.h> void DB::get(int f, int i)
class DB; {
class DM feet =f;
{ inches=i;
int meters; }
int centimeters; void DB::show()
public: {
void get_dist(int m, int c); cout<<"Distance in feet = "<<feet<<"
void show_dist(); inches= "<<inches<<endl;
friend void add(DM ob1, DB ob2); }
}; void add(DM ob1, DB ob2)
class DB {
{ //int cent =
int feet; ob1.meters*100+ob1.centimeters;
int inches;
public: int in=ob2.feet*12+ob2.inches;

Gurpreet Sir….. Page 13


int cm=in*2.54;

ob1.meters +=cm/100;
ob1.centimeters +=cm%100;

cout<<"After Addition
..."<<endl<<"Equivalent Distance in
Meter="<<ob1.meters<<endl;
cout<<"Centimeters="<<ob1.centimeters<<
endl;
}
void main()
{
clrscr();
DM ob1;
ob1.get_dist(4,10);
ob1.show_dist();
DB ob2;
ob2.get(7,9);
ob2.show();
add(ob1,ob2);
getch();
}

Gurpreet Sir….. Page 14


Write a program to read the contents of a Write a program that reads a text file and
text file and count the number of words creates another file that is identical except
present in the file. that every sequence of consecutive blank
spaces is replaced by single space.
#include<fstream.h>
#include<iostream.h> #include<fstream.h>
#include<conio.h> #include<iostream.h> #include<conio.h>
void main() void main()
{ {
clrscr(); clrscr();
ifstream fin; ifstream fin;
fin.open("text.txt"); fin.open("source.txt");
int count; ofstream fout;
count=0; fout.open("target.txt");
char ch; char ch,ab;
while(fin.eof()==0) ab=' ';
{ while(fin.eof()==0)
ch=fin.get(); {
if(ch==' ') ch=fin.get();
count++; if(ab==ch) {
} }
else {
cout<<"Total Words available in file are: fout.put(ch);
"<<count+1; cout<<ch;
fin.close(); }
getch(); ab=ch;
} }
fin.close();
fout.close();
getch();
}

Gurpreet Sir….. Page 15


Write a program that will ask the users to void main()
enter the details of 5 students and transfer {
those details into a binary file Stud.dat. fstream finout;
Write another file that will read the details finout.open("stud.dat",ios::out);
of the students and print the names of all student s[5];
those students who have total marks greater for(int i=0;i<5;i++)
that a particular given value. {
cout<<"Enter Detail of
#include<fstream.h> "<<i+1<<"student"<<endl;
#include<iostream.h> s[i].get_data();
#include<conio.h> finout.write((char*)(&s[i]),sizeof(s[i]));
class student }
{ finout.close();
char name[20]; finout.open("stud.dat",ios::in);
public: float target;
float marks; cout<<"Enter the target value: "<<endl;
void get_data() cin>>target;
{ cout<<"Details of student whose marks
cout<<"Enter Name: "; greater then: "<<target<<" are"<<endl;
cin>>name; for( i=0;i<5;i++)
cout<<"Enter Marks: "; {
cin>>marks; //cout<<"Detail of "<<i+1<<" student" ;
} finout.read((char*)(&s[i]),sizeof(s[i]));
void show_data() if(s[i].marks>target) {
{ s[i].show_data();
cout<<"Name: ="<<name<<endl; }
cout<<"Marks: ="<<marks<<endl; }
} finout.close();
}; getch();
}

Gurpreet Sir….. Page 16


Write a program that will take two strings
from the command line as argument and
print the appropriate message if both
the strings are same.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main(int argc,char *argv[])
{
clrscr();
cout<<"first String from command line is:
"<<argv[1]<<endl;
cout<<"second String from command line
is: "<<argv[2]<<endl;

int c=strcmp(argv[1],argv[2]);
if(c==0)
cout<<"Both String from command line
argument are Equal";
else
cout<<"Both String from command line
argument are not Equal";
getch();
}

Gurpreet Sir….. Page 17

You might also like