This is a sample program on encapsulation, Using encapsulation here we display the details of a book and compute costs.
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class book
{
int bookno;
float price;
char booktitle[100];
float total_cost(int n)
{
return (price*n);
}
public:
void input()
{
cout<<"Enter Book Number, Price & Name Of Book";
cin>>bookno>>price>>booktitle;
}
void purchase()
{
int n;
cout<<"Enter Number Of Copies:";
cin>>n;
cout<<"Total Cost:"<<total_cost(n);
}
};
void main()
{
clrscr();
book B;
B.input();
B.purchase();
getch();
}
OUTPUT:
Enter Book Number, Price & Name Of Book
15422
50
Two States
Enter Number Of Copies: 2
Total Cost: 100
Comments
Post a Comment
Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...
Experienced guys can share their resumes at admin@interview-made-easy.com