T
Tommy Lang
Hi everybody!
I am trying to learn the basics of C++ myself and have a hard time
understanding some stuff like pointers and references etc.
I have created a small program that adds two numbers and prints the
result on the screen.
The program works just fine and I am proud of it .
But now I want to make some changes to it and only use dynamic
variables (and make use of *, &, new...).
I know from books that you create dynamic variables like this:
int *pt = new int; //pt is a pointer to an integer right
and then kill it like this;
delete pt;
But when I try to put some * in my program it just dies or I get like
a million errors. It's terrible.
Could anybody give me help, hints, pointers...
Thankful for any help on this,
Tommy
//--------MY PROGRAM-------------
#include <iostream.h>
class MyClass {
private:
int num1, num2, sum;
public:
MyClass();
void add(int n1, int n2);
void print();
};
MyClass::MyClass(){num1=1;num2=1;}
void MyClass::add(int n1, int n2){sum = n1 + n2;}
void MyClass:rint(){cout << sum << endl;}
void main(){
MyClass mc;
int x,y;
cout << "Number 1: ";cin >> x;cin.get();
cout << "Number 2: ";cin >> y;cin.get();
mc.add(x,y);
mc.print();
}
I am trying to learn the basics of C++ myself and have a hard time
understanding some stuff like pointers and references etc.
I have created a small program that adds two numbers and prints the
result on the screen.
The program works just fine and I am proud of it .
But now I want to make some changes to it and only use dynamic
variables (and make use of *, &, new...).
I know from books that you create dynamic variables like this:
int *pt = new int; //pt is a pointer to an integer right
and then kill it like this;
delete pt;
But when I try to put some * in my program it just dies or I get like
a million errors. It's terrible.
Could anybody give me help, hints, pointers...
Thankful for any help on this,
Tommy
//--------MY PROGRAM-------------
#include <iostream.h>
class MyClass {
private:
int num1, num2, sum;
public:
MyClass();
void add(int n1, int n2);
void print();
};
MyClass::MyClass(){num1=1;num2=1;}
void MyClass::add(int n1, int n2){sum = n1 + n2;}
void MyClass:rint(){cout << sum << endl;}
void main(){
MyClass mc;
int x,y;
cout << "Number 1: ";cin >> x;cin.get();
cout << "Number 2: ";cin >> y;cin.get();
mc.add(x,y);
mc.print();
}