Wednesday 6 July 2016

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 and placing the response in the uninitialized value,and then displaying this result.

#include <iostream>
#include <conio.h>
using namespace std;
class Int{
private:
int intvar;
public:
Int(){
intvar = 0;
}
Int(int x){
intvar = x;
}
void display(){
cout << intvar << endl;
}
void add(Int x, Int y){
intvar = x.intvar + y.intvar;
}
};
int main(){
Int a(5),b(45);
Int c;
c.add(a,b);
c.display();
}

7 comments:

  1. Can u make the continution of this code by overloading arthematic operators so that they operate on object type int

    ReplyDelete
  2. how it can be done in python?

    ReplyDelete
  3. how to do same question with getters and setter

    ReplyDelete
  4. can you do it in more ez way?

    ReplyDelete