Comments

Thursday, November 8, 2012

OOP Add Num of two Object initailize in Constructor

Posted by at 7:59 AM Read our previous post

Object
Create a class that imitates part of the functionality of the basic data type int. Call the
class Int (note different capitalization). The only data in this class is an int variable.
Include member functions to initialize an Int to 0, to initialize it to an int value, to display
it (it looks just like an int), and to add two Int values.
Write a program that exercises this class by creating one uninitialized and two initialized
Int values, adding the two initialized values

Source Code
#include <iostream>

using namespace std;

class Integer

{

private:

    int num,num1;

public:

    Integer()

    {}

    Integer(int n)

    {

    num=n;

    }

 

    int add(Integer Int1,Integer Int2)

    {

    num1=Int1.num+Int2.num;

    return num1;

 

    }

};

void main()

{

    int number;

    Integer Int1(90),Int2(78),Int3;

    number=Int3.add(Int1,Int2);
    cout<<"1st object number: "<<90<<"\n2nd object number: "<<78<<endl;
    cout<<"\n3rd ojbect is sum of 1st and 2nd object: "<<number<<"\n";

}
Output

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Popular Posts

Labels

© Codepirate is powered by Blogger - Template designed by Stramaxon - Best SEO Template