Saturday, 20 January 2018

CPP-Slip23



Slip23

Q.1)   Write the definition for a class called ‘point’ that has x & y as integer data members. Use copy constructor to copy one object to another. (Use Default and parameterized constructor to initialize the appropriate objects)
Write a C++ program to illustrate the use of above class.                             

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class point
{
int x,y;
public:
point()
{
}
point(int a,int b=200)
{
x=a;y=b;
}
void operator=(point ob2)
{
x=ob2.x;
y=ob2.y;
}
void display()
{
cout<<"\nx = "<<x<<"\t y = "<<y;
}
};

void main()
{
point ob1(100,200),ob2;
int n,m;
clrscr();

               
ob1.display();
    
ob2=ob1;
cout<<"\nAfter overload = operator .\n";
ob2.display();
cout<<"\nEnter number : ";
cin>>n;
cout<<"\nEnter number : ";
cin>>m;
point ob3(n,m);
ob3.display();
ob2=ob3;
cout<<"\nAfter overload = operator .\n";
ob2.display();

getch();
}


Q.2)     Create a C++ class MyFile containing:
                        - FILE *fp;
                        - Char filename[maxsize];
            Write necessary member Functions using operator overloading:
<<  To display the contents of a file,>>  To write the contents into a file

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<ctype.h>

class MyFile
{
int rno;
char name[30];
public:
MyFile(){}
MyFile(int r,char nm[])
{            
rno=r;
strcpy(name,nm);
}
friend ofstream& operator<<(ofstream & ,MyFile &);
friend ifstream& operator>>(ifstream & ,MyFile &);
};

ofstream& operator<<(ofstream &out,MyFile &f)
{
out<<f.rno<<"\t"<<f.name<<endl;
return out;
}

ifstream& operator>>(ifstream &in,MyFile &f)
{
in>>f.rno>>f.name;
return in;
}

void main()
{
MyFile ob(1,"ABC"),ob2(2,"XYz");
ofstream fout("Slip23.txt");
fout<<ob<<ob2;
fout.close();
ifstream fin("Slip23.txt");
fin>>ob>>ob2;
fin.close();
getch();
}