20120124

How to pass a object and return a object in C++/Cpp

//How to pass a object and return a object in C++/Cpp
//Program to show how to pass a object and return a object
// How to use friend function in C++/ Cpp
#include<iostream>
using namespace std;
class complex
{
private:
        float x;
        float y;
public:
        void getdata(float real, float imag)
        {
        x=real;
        y=imag;
        }
        friend complex sum(complex, complex);
        void show(complex);
};
//defining friend function outside the class
 //does not require scop resolution operator
complex sum(complex C1, complex C2)// friend function defined outside the class
//passing objects as function arguments
{
complex C3;
C3.x= C1.x + C2.x;
C3.y= C1.y + C2.y;
return(C); //returning object C

How to acces private member function in C++/CPP

How to acces private member function in C++/CPP
Program to show accessibility of private member function.
//private member function(skeleton) can be accessed using
//member function only and not by object and dot operator
#include<iostream>
using namespace std;
class item
{
private:
        int number;
        float price;
        void getdata(int a, float b) // here member function getdata
                                               //is private so not accessible by object

20120123

Why destructor should be virtual in C++ ?

Why destructor should be virtual in C++ ?
Memory Leak ?

When delete is called virtual destructor makes sure to release memory of first derived class object then of by base class object. If destructor is not virtual then only the base class object's memory get released and not that of derived class object.
example:
#include<iostream>
using namespace std;
class base
{
  base(){};
 ~base(){};
};
class derived:public
{
  derived(){};
 ~derived(){};
};
int main()
{
base *ptr= new derived(); //exp1
delete ptr; //exp2
}
Explanation:
Line exp1 makes the derived and base constructoes to be called to allocate memory for base as well derived class objects.
But, if destructor is not virtual in base class then
Line exp2 makes to call the base destructor and derived destructor never gets called and hence memory allocated to derived class object is not release and leads to memory leak because memory used by derived class object can not be used util the end of the program.
Simples Way to learn Data Structure in C++ CPP or C
C++ Program for Circular Queue implementation using linked list
 
C++ program for Circular Queue implementation using Array

C++ program for reversing single Linked list nodes 

C++ Program for sorted single linked list with class

C++ Program for BST-Binary Search Tree   


Previous                             Home                               Next

20120118

How to use tricky while loop in CPP,C++,C

#include<stdio.h>
int main()
{
int i=0;
while(i<5)
        {
        printf("\n Iteration No=%d",i);
        }
return 0;
}
////////////////////
o/p = infinite times Iteration No=0 as we are not incrementing the i
///////////////////

How to use for loop in C++ CPP C

//==========================
//What will be the out put in C++, C or CPP
//1.Tricky for loop program
===========================
#include<stdio.h>
int main()
{
int i;
for(i=0;i<=5;i++); //when i=6 condition will become false and
                              //control flow will go next stmt to print once
        {