Category Archives: Class

C++ Object Slicing with Example

In this tutorial, we are going to see how object slicing in C++. Before diving into the topic let us revise two simple concepts of object oriented programming. Inheritance Polymorphism Inheritance is said to be an IS-A relationship. For e.g. Here Tiger class will inherit all the properties of the animal class and will add… Read More »

Difference between Class and Structure

Here you will learn about difference between class and structure. A class and a structure, both are user defined data types and understanding the difference between both of them might be confusing at times. In order to understand this, the following table shows clear differences between the two. Class Structure It is a reference data… Read More »

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; }