0% found this document useful (0 votes)
56 views5 pages

Oop PR6

This document discusses classes and objects in C++. It provides examples of code to [1] declare a Book class with data members and functions to accept and display book data, and [2] declare a Staff class with data members and functions to accept staff data, calculate salaries, and display results. It also [3] provides incomplete code examples and asks the reader to justify the expected output.

Uploaded by

Ashwath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views5 pages

Oop PR6

This document discusses classes and objects in C++. It provides examples of code to [1] declare a Book class with data members and functions to accept and display book data, and [2] declare a Staff class with data members and functions to accept staff data, calculate salaries, and display results. It also [3] provides incomplete code examples and asks the reader to justify the expected output.

Uploaded by

Ashwath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical No.

6: Program to Implement Classes and Objects

Practical Related Questions


1. What is the difference between struct and class in C++?
Structure Class
1. It contains logically related data 1. Data and functions that operate
items which can be of similar type on data are tied together on a data
or different type. structure called class.
2. It is declared using struct keyword. 2. It is declared using class
keyword.
3. Syntax- struct structure_name 3. Syntax- class class_name
{ {
Datatype variable_name; Access specifier:
Datatype variable_n; Declare data members;
} Structure_variable; Declare member functions;
};

2. State output of the following code:


#include<iostream.h>
Class Empty {};
Void main ()
{
Cout<<sizeof(Empty);
}
Ans) a) non-zero value (1)

3. State True or False: Data items in C++ are by default public .


Ans) False

Exercise
1. Write a C++ program to declare a class “Book” having data members
book_name, author and price. Accept and display data for book having
minimum price.
Code:
#include<iostream.h>
#include<conio.h>
class Book
{ public:
char book_name[20];
char author[20];
int price;

void getdata()
{
cout<<"Enter book name, Author's name and price of the book:
"<<endl;
cin>>book_name>>author>>price;
}
void putdata()
{
cout<<"Book Name:"<<book_name<<endl;
cout<<"Author Name:"<<author<<endl;
cout<<"Price of the book:"<<price<<endl;
}
};
void main()
{
Book b1,b2;
clrscr();
b1.getdata();
b2.getdata();
cout<<"Output prepared Ashwath Bhekare (CO3IB) 22203B0015"<<endl;
if (b1.price > b2.price)
{
b1.putdata();
}
else
{
b2.putdata();
}
getch();
}
Output:
2. Write a C++ program to declare a class “staff” having data members name,
basic salary, DA, HRA and calculate gross salary. Accept and display data for one
staff.
a. Where DA=74.5% of basic.
b. HRA=30% of basic.
c. Gross_salary= basic+HRA+DA
CODE:

#include<iostream.h>
#include<conio.h>
class staff
{
float basic_salary, DA, HRA, gross_salary;
char name[20];

public:
void getdata()
{
cout<<"Enter Staff name and basic salary: "<<endl;
cin>>name>>basic_salary;
}
void cal()
{ DA=0.745*basic_salary;
HRA=0.3*basic_salary;
gross_salary=basic_salary+DA+HRA;
}
void putdata ()
{
cout<<"Staff name:"<<name<<endl;
cout<<"Gross salary: "<<gross_salary<<endl;
}
};
void main()
{
staff s1;
clrscr();
cout<<"Output prepared by Ashwath Bhekare (CO3IB)
22203B0015"<<endl;
s1.getdata();
s1.cal();
s1.putdata();
getch();
}
OUTPUT:

3. Complete the given table:

Program Code Write & justify Output


a) #include<iostream.h> Illegal structure operation
class Empty{};
void main()
{
Empty a, b;

if (a == b)

cout<<"impossible"<<endl;

else
cout<<"Fine "<<endl;
}
Semicolon after int (area) function. (int (area);)
b) After solving error: area:12

{
#include<iostream.h>
class Rectangle

int width, height;


public:
void set_values
(int,int);
int area();
{return width*height;}
};
void Rectangle::set
_values (int x, int y)
{ width=x;
height=y;
}
void main() {
rectangle rect;
rect.set_values (3,4);

cout<<"area:"<<rect.area();
}

You might also like