By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Dynamic variables

P: n/a
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::print(){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();
}
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tommy Lang wrote:

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...


There are more ways to incorrectly sprinkle *'s into code
then there are stars in the sky. Whilst it would be possible
to rewrite your code using *, &, new, and delete it would be
better to post an example of what you are doing wrong.

Jul 19 '05 #2

P: n/a
Tommy Lang wrote:
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> Prefer to use the more recent header files (without extensions):
#include <iostream>
using namespace std; // for cout, cin and endl.

class MyClass {
private:
int num1, num2, sum; Prefer one varable declaration per line:
int num1;
int num2;
int sum;
If the integer values will only be positive,
prefer using "unsigned int".

public:
MyClass(); Beware that since you have not declared or defined a
copy constructor or a destructor, the compiler will
create them for you.

void add(int n1, int n2);
void print(); If a method does not modify the class variables, often called
the objects state, prefer to make it constant:
void print() const;
};

MyClass::MyClass(){num1=1;num2=1;} Prefer to use multiple lines when defining a method
or function.
MyClass::MyClass()
{
num1 = 1;
num2 = 1;
}
Also prefer initialization lists:
MyClass::MyClass()
: num1(1), num2(1), sum(0)
{
}

void MyClass::add(int n1, int n2){sum = n1 + n2;} Again, use multiple lines.
This function may confuse people since it uses the
sum member variable, but not the num1 and num2
member variables.

void MyClass::print(){cout << sum << endl;} Use multiple lines. The compiler doesn't care
how many lines you use, because they are all
considered as "white space". Many newlines
are treated as one white space.

void main(){ The main() function returns int. Always.

MyClass mc;
int x,y; One declaration per line is preferred.

cout << "Number 1: ";cin >> x;cin.get();
cout << "Number 2: ";cin >> y;cin.get(); Prefer one statement per line.
What is the "cin.get()" used for?
mc.add(x,y);
mc.print(); The main function requires a value to return to
the operating system. Here are a few examples:
return 0; // Zero indicates success.
return EXIT_SUCCESS; // defined in <cstdlib>
return EXIT_FAILURE; // defined in <cstdlib> }


Adopt a formatting style and stick with it. There
are many out there, none better than the rest.
Choose one you are comfortable with or create your
own by adopting pieces from various styles. In
any case, be consistent.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.