slip 23

/* Slip no : 23_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)
                {
                                x=a;
                                y=b;
                }

                point(point &ob2)
                {
                                x=ob2.x;
                                y=ob2.y;
                }

                void display()
                {
                                cout<<"\nx = "<<x<<"\t y = "<<y;
                }
};

void main()
{
                point ob1(100,200);
                int n,m;
                clrscr();
                ob1.display();
                point ob2(ob1);
                cout<<"\nAfter copy comstructor .\n";
                ob2.display();

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

                getch();

}








/* Slip no : 23_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();
}
slip 23 slip 23 Reviewed by Dinesh Varal on January 27, 2018 Rating: 5

No comments:

Powered by Blogger.