Showing posts with label CPP. Show all posts
Showing posts with label CPP. Show all posts

Thursday, 1 October 2015

Adding arrays from different classes using friend function [cpp]

Q:write a CPP program to declare 3 classes perform the addition of the data members in first 2 classes and result in the 3rd class using friend function

DEFINITION:
friend function:In object-oriented programming, a friend function that is a "friend" of a given class is allowed access to private and protected data in that class that it would not normally be able to as if the data was public . Normally, a function that is defined outside of a class cannot access such information.

PROGRAM:

#include <iostream>
#include<conio.h>
using namespace std;
void add();
class A{
    int a[10],limit;
public:
    A(int m){
        limit=m;
        cout<<"enter array"<<endl;
        for(int i=0;i<limit;i++)
        cin>>a[i];
    }
    friend void add();


};

class B{
    int a[10],limit;
public:
    B(int m){
        limit=m;
        cout<<"enter array"<<endl;
        for(int i=0;i<limit;i++)
        cin>>a[i];
    }
    friend void add();


};

class C{
    int a[10],limit;
public:
    C(){
        limit=0;
        }
    friend void add();

    void addToC(int k){
    a[limit]=k;
    limit++;
    }
    void showC(){
    for(int i=0;i<limit;i++)
        cout<<a[i]<<" ";
    }
};

int main()
{

    add();
}

void add(){
    int limit;
    cout<<"limit:";
    cin>>limit;
    A a(limit);
    B b(limit);
    C c;
    for(int i=0;i<limit;i++)
            c.addToC(a.a[i]+b.a[i]);
    c.showC();

}

Tuesday, 12 May 2015

[CPP] STACK CLASS THAT CAN HANDLE INT AS WELL AS CHAR (& MORE) - GENERIC CLASS

DESCRIPTION 

 A C++ program to implement a generic stack class.  Include a default constructor, destructor and member functions  push( ) to put values into the stack, pop( ) to remove values from stack, isempty( ) and isfull( ) for testing whether stack is empty or full. Use array implementation of stack. Write a main program to test your class for integer and character type data.

PROGRAM


#include <iostream>
using namespace std;


template<class T>
class stack{

private:
    T a[10];
    int top;

public:
  stack(){
        top=-1;
    }

    int isfull(){
        if(top==9)
           return 1;
           return 0;
                } 


Saturday, 9 May 2015

[ C++ ] WHAT IS THE DIFFERENCE BETWEEN INLINE FUNCTIONS AND MACROS?

Both Inline function  and Macro works the same in almost all scenarios there  are lot of key difference between them.
Before we get into those topics what is this inline and macro actually?
They are kind of like functions but instead of control passing which happens in functions these will replace the actual line of call by the function body..

for example

--------------------------------------------------------------------------------------------------------------------------

MACRO:

    #define MAX(x,y) x>y?x:y

would give the largest among 2 parameters on the call of  MAX(a,b)

If we are to write 
                  
                          int j=MAX(2,3);

  The preprocessor would replace the statement as
                           
                           int j=3;
before execution


INLINE:



inline int add(int a, int b)

    {

    return (a+b);
    
      }



after compiling the code each and every call of this function add in the program is replaced by the body of function

Thursday, 7 May 2015

[C++] CALL BY ADDRESS AND CALL BY REFERENCE FOR SWAPING


CALL BY ADDRESS




PROGRAM

#include <iostream>
#include <conio.h>

void main()
{
int a = 10, b = 5;
swap(&a,&b);
cout << a << "&" << b;

}

void swap(int *m,int *n){
int temp;
temp = *m;
*m = *n;
*n = temp;
}

OUTPUT


  • 5&10


                              CALL BY REFERENCE


PROGRAM

#include <iostream>
#include <conio.h>

void main()
{
int a = 10, b = 5;
swap(a,b);
cout << a << "&" << b;

}

void swap(int &m,int &n){
int temp;
temp = m;
m = n;
n = temp;
}


OUTPUT

  • 5&10