C++

C++ Programming

Slip20

Q.1) Write a C++ program to calculate maximum of two integer numbers of two different
classes using friend function. [Marks 15]

Solution

#include<iostream.h>
#include<conio.h>
class second;                // forward declaration
class first
{
  private:
  int x;
  public:
void accept()
{
  cout<<"Enter the  value of x:";
  cin>>x;
}
friend void max(first f1,second s);
};
class second
{
 private:
int y;
 public:
void accept()
{
cout<<"Enter the  value of y:";
  cin>>y;
}
friend void max(first f1,second s1);
};
void max(first f1,second s1)
{
 if(f1.x>s1.y)
 {
  cout<<f1.x<<" is maximum";
 }
 else
 {
  cout<<s1.y<<" is maximum";
 }
}
void main()
{
 clrscr();
 first f1;
 second s1;
 f1.accept();
 s1.accept();
 max(f1,s1);
 getch();
}


Output

Enter the  value of x:25
Enter the  value of y:20
25 is maximum


Q.2) Write a C++ program to create a class Department which contains data members as
Dept_Id, Dept_Name, H.O.D., Number_Of_staff. Write necessary member functions to
i. Accept details from user for ‘ n’ departments and write it in a file
“ Dept.txt” .
ii. Display details of department from a file.
iii. Count the number of objects stored in a file. [Marks 25]

Solution

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class dept
{
int id,no_of_staff;
char *name,*HOD;
int static count;
public:
void accept()
{
cout<<"enter id name hod,no of staff";
cin>>id>>name>>HOD>>no_of_staff;
count++;
}
void display()
{
cout<<"dept id="<<id<<endl;
cout<<"name="<<name<<endl;
cout<<"hod="<<HOD<<endl;
cout<<"no of staff="<<no_of_staff<<endl;


}
static void show()
{
   cout<<"total no of object created"<<count<<endl;
}


};
int dept::count;
int main()
{
clrscr();
dept obj[10];
int no;
fstream file;
file.open("dept.txt",ios::in | ios::out);
cout<<"how many dept";
cin>>no;
for(int i=0;i<no;i++)
{
obj[i].accept();
file.write((char *)&obj[i],sizeof(obj[i]));
}
cout<<"file created succ....\n";
file.seekg(0);
cout<<"output\n";
for(i=0;i<no;i++)
{
file.read((char *)&obj[i],sizeof(obj[i]));
obj[i].display();
dept::show();
}
getch();
return 0;

}


Output

how many dept 3
enter id name hod,no of staff
41140
computer
Akshay
30
enter id name hod,no of staff
41142
Electronic
Ravi 25
enter id name hod,no of staff
41148
Mechnical
Girish
35
file created succ....
output
dept id=-24396
name=computer
hod=Akshay
no of staff=30
total no of object created3
dept id=-24394
name=Mechnical
hod=Ravi
no of staff=25
total no of object created3
dept id=-24388
name=Mechnical
hod=Girish
no of staff=35
total no of object created3
Null pointer assignment

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