Category Archives: Class

Difference Between Constructor and Destructor

Constructor and Destructor are the special methods which makes our crucial tasks easier while programming. If you have ever worked in programming language like C++ then you must have encountered these two terms. Being methods, they clearly let us implement the reusability of the code. But the talk of the town (esp. for the beginners)… Read More »

C++ Program That Defines a Class String and Overload == Operator to Compare Two Strings [Operator Overloading Concept]

#include<iostream> #include<stdio.h>                             //used for gets() #include<string.h>                           //used for strcmp() using namespace std; class String {         char str[20];         public:         void getdata()                            //function to read the string         {                 gets(str);         }         //operator function to overload comparison operator and compare two strings         int operator ==(String s)        … Read More »

C++ program to swap two numbers using class

#include<iostream.h> #include<conio.h> class swap { int a,b; public: void getdata(); void swapv(); void display(); }; void swap::getdata() { cout<<“Enter two numbers:”; cin>>a>>b; } void swap::swapv() { a=a+b; b=a-b; a=a-b; } void swap::display() { cout<<“a=”<<a<<“tb=”<<b; } main() { clrscr(); swap s; s.getdata(); cout<<“nBefore swap:n”; s.display(); s.swapv(); cout<<“nnAfter swap:n”; s.display(); getch(); return 0; }