Pages

Showing posts with label CONSTRUCTOR. Show all posts
Showing posts with label CONSTRUCTOR. Show all posts

Tuesday, September 18, 2012

C++ PROGRAM TO CREATE A CLASS CALLED STRING.

Write a C++ program to create a class called STRING and implement the following operations. Display the results after every operation by overloading the operator.
 
STRING s1 = “HELLO”

STRING s2 = “WORLD”

STIRNG s3 = s1 + s2 ; (Use copy constructor).

 
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
  char name[23];
  public :string()
  {
    name[23]='\0';
  }
  string(char s[])
  {
    strcpy(name,s);
  }
  string(string &s)
  {
    strcpy(name,s.name);
  }
friend string operator +(string s1, string s2);
friend ostream &operator <<(ostream  &out, string &s);
};
ostream &operator <<(ostream &out , string &s)
{
  out <<"\t"<<s.name<<endl;
  return(out);
}
string operator +(string s1, string s2)
{
  string temp(s1);
//strcat(temp.name,"");
  strcat(temp.name, s2.name);
  return(temp);
}
void main()
{
  clrscr();
  string s1("hello ");
  string s2("world");
  string s3=s1+s2;
  cout<<"\nFIRST STRING ="<<s1
      <<"\nSECOND STRING ="<<s2;
  cout<<"\nCONCATENATED THIRD STRING ="<<s3;
  getch();
}


OUTPUT


Sunday, September 16, 2012

C++ PROGRAM TO CREATE A CLASS OCTAL.

Write a C++ program to create a class called OCTAL which has the characteristics of an octal number. Implement the following operations by writing an appropriate constructor and an overloaded operator +.
i) OCTAL h = x; where x is an integer.
ii) int y = h + k; where h is an OCTAL object and k is an integer.



Display the OCTAL result by overloading the operator <<. Also display the values of h and y.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
class OCTAL
  {
    int oct[15],count;
    public:
       OCTAL(int x);
       int operator +(int);
       friend ostream &operator<< (ostream &,OCTAL &);

  };
OCTAL ::OCTAL(int x)
   {
     int i=0,rem,a[15];
     while(x!=0)
       {
     rem=x%8;
     x=x/8;
     a[i++]=rem;
       }
       count=i;
       int n=count-1;
    for(i=0;i<count;i++)
      {
        oct[i]=a[n];
        n--;
      }
   }

int OCTAL::operator+(int k)
   {
      int x=0,i;
      int j=count-1;
    for(i=0;i<count;i++)
      {
        x=x+oct[j]*pow(8,i);
        j--;
      }
      return(x+k);
   }

ostream & operator<<(ostream &print,OCTAL &o)
   {
    for(int i=0;i<o.count;i++)

        print<<o.oct[i];
      return(print);
    }
void main()
   {
     int x,k,y=0;
     clrscr();
     cout<<"\nEnter the integer value in decimal: ";
     cin>>x;
     OCTAL h=OCTAL(x);
     cout<<"The corresponding octal value "

         <<for ("<<x <<" )is: "<<h;
     cout<<"\n\nEnter the integer to be added to octal value:";
     cin>>k;
     y=x+k;
     cout<<"\n "<<h<<" (Octal)+"<<k<<" (Decimal)="

         <<y<<" (Decimal)";
     getch();
   }

OUTPUT



Monday, September 10, 2012

C++ PROGRAM TO DEMONSTRATE DEFAULT AND PARAMETERISED CONSTRUCTORS.

Define the class Person which has name (char*) and age (int). Define the following constructors
a. default
b. with name as argument (assume age is 18)
c. with name and age as argument
Also define the copy constructor.



#include<iostream.h>
#include<string.h>
#include<conio.h>
class person
{
  char *name;
  int age;
  public:
  person()
  {
    name=NULL;
    //strcpy(name,NULL);
    age=0;
  }
  person(char *n)
  {
    strcpy(name,n);
    age=18;
  }
  person(char *n1,int a)
  {
    strcpy(name,n1);
    age=a;
  }
  person(person &p)
  {
    strcpy(name,p.name);
    age=p.age;
  }
  void disp()
  {
    cout<<"name is "<<name<<endl;
    cout<<"age is "<<age<<endl;
  }
};
void main()
{
  clrscr();
  person p1("ram"),p2("sita",20),p3=p2;
  cout<<"hello\n";
  p1.disp();
  p2.disp();
  p3.disp();
  getch();
}


OUTPUT