Slip28 C++

C++ Programming

Slip28

Q.1) Create a class Point that has x & y as integer data members. Write a C++ program to
perform following functions:
void setpoint(int, int) To set the specified values of x and y in object.
void showpoint() To display contents of point object.
point addpoint(point) To add the corresponding values of x, and y in point
object argument to current point object return point.
[Marks 15]

Solution

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class point
  {
   int x,y;
public:
   void setpoint()
{
   cout<<"Enter value x & y: ";
   cin>>x>>y;
}
   void showpoint()
{
cout<<"\n"<<x<<"\t"<<y;
}
  point addpoint(point X)
{
  point Z;
  Z.x=x+X.x;
  Z.y=y+X.y;
  return(Z);
}
   };
void main()
{
point X,Y,Z;
clrscr();
X.setpoint();
Y.setpoint();
Z=X.addpoint(Y);
clrscr();
X.showpoint();
Y.showpoint();
Z.showpoint();
getch();
}


Output

3       5
4       6
7       11



Q.2) Create a base class Media. Derive two different classes Book (Book_id, Book_name,
Publication, Author, Book_price) and CD (CD_title, CD_price) from Media. Write a C++
program to accept and display information of both Book and CD. (Use pure virtual
function) [Marks 25]

Solution

#include<iostream.h>
#include<conio.h>
class media
{
public:
   virtual void accept()=0;
   virtual void display()=0;
};
class book : public media
{
int no;
char nm[10];
char publication[10];
char author[10];
int price;
public:
void accept()
{
   cout<<"Enter ID: ";
   cin>>no;
   cout<<"Enter Title : ";
   cin>>nm;
   cout<<"Enter publication: ";
   cin>>publication;
   cout<<"Enter author : ";
   cin>>author;
   cout<<"Enter price: ";
   cin>>price;
}
void display()
{
cout<<endl<<"ID :"<<no<<endl;
cout<<"Title: "<<nm<<endl;
cout<<"Publication: "<<publication<<endl;
cout<<"Author : "<<author<<endl;
cout<<"Price: "<<price<<endl;
}
};
class CD : public media
{
char title[10];
int price;
public:
void accept()
{
cout<<endl<<"Enter title:";
cin>>title;
cout<<"Price:";
cin>>price;
}
void display()
{
cout<<endl<<"Title: "<<title<<endl;
cout<<"Price: "<<price<<endl;
}
};
int main()
{
int n;
media *ptr;
book b;
CD c;
cout<<"Enter record : ";
cin>>n;
ptr=new book[n];
ptr=&b;
for(int i=0;i<n;i++)
{
ptr->accept();
}
for(i=0;i<n;i++)
{
ptr->display();
}
ptr= new CD[n];
ptr=&c;
for(i=0;i<n;i++)
{
ptr->accept();
}
for(i=0;i<n;i++)
{
ptr->display();
}
getch();
return 0;
}


Output

Enter record : 1
Enter ID: 123                                                                   
Enter Title : C++                                                               
Enter publication: Nirali                                                       
Enter author : Gajanan Deshmukh                                                 
Enter price:                                                                    
ID :123                                                                         
Title: C++                                                                      
Publication: Nirali                                                             
Author : Gajanan                                                                
Price: 0                                                                        
                                                                                
Enter title:Price:                                                              
Title:                                                                          
Price: 0                                                                       
BCA Pratical Solution

My name is Vivek And I from Mumbai and Complete my Graduation Bca.my Age is 23 Years.

Post a Comment

Previous Post Next Post