Object Oriented Programming Practical 16

Question 1
Write a C++ program to declare a class "Book" containing data members book_name, auther_name, and price . Accept this information for one object of the class using pointer to that object.
book.cpp
C
				#include<iostream.h>
#include<stdio.h>
#include<conio.h>

class book
{
   public:
	int price;
	char book_name[50],author_name[50];

	void accept()
   {
		cout<<"\nEnter book name: ";
		gets(book_name);
		cout<<"Enter Book author: ";
		gets(author_name);
		cout<<"Enter price per copy: ";
		cin>>price;
	}

	void display()
	{
		cout<<"\nBook name is: "<<book_name<<endl;
		cout<<"Book author is: "<<author_name<<endl;
		cout<<"Book price is: "<<price<<endl;
	}
};

void main()
{
	clrscr();
	book b,*bptr;
	bptr=&b;
	bptr->accept();
	bptr->display();
	getch();
}

			
Output
Question 2
Write a C++ program to declare a class "Box" having data members height, width and breadth. Accept this information for one object using pointer to that object . Display the area and volume of that object.
box.cpp
C
				#include<iostream.h>
#include<conio.h>

class box
{
	int height,width,breadth;
	public:
	void accept()
	{
		cout<<"\nEnter height of box: ";
		cin>>height;
		cout<<"\nEnter width of box: ";
		cin>>width;
		cout<<"\nEnter breadth of box: ";
		cin>>breadth;
	}

   void display()
	{
		int area=(height*width+width*breadth+breadth*height)*2;
		int volume=height*width*breadth;
		cout<<"\nArea of box: "<<area;
		cout<<"\nVolume of box: "<<volume;		
	}
};

void main()
{
	clrscr();
	box b,*bptr;
	bptr=&b;
	bptr->accept();
	bptr->display();
	getch();
}

			
Output
Question 3
Write a C++ program to declare a class birthday having data members day, month, year. Accept this information for five objects using pointer to the array of objects
birthday.cpp
C
				#include<iostream.h>
#include<conio.h>

class birthday
{
	int day,month,year;
	
	public:
	void accept()
	{
		cout<<"\nEnter day: ";
		cin>>day;
		cout<<"\nEnter month: ";
		cin>>month;
		cout<<"\nEnter year: ";
		cin>>year;
	}

   void display()
	{
		cout<<"\nBirth date is: "<<day<<"/"<<month<<"/"<<year;
	}
};

void main()
{
	clrscr();
	int i;
	birthday b[5],*bptr;
	bptr=&b[0];
	for(i=0;i<5;i++)
	{
		bptr->accept();
		bptr++;
	}
	bptr=&b[0];
	for(i=0;i<5;i++)
	{
		bptr->display();
		bptr++;
	}
	getch();
}

			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *