473,473 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamic variables

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
2 8392
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: propizzy | last post by:
Appreciate any help!!! PROBLEM: I have this form that allows the user to dynamically create additional fields (see javascript code bellow). I am trying to retrieve the values entered into these...
1
by: Tommy Lang | last post by:
I am trying to learn to use dynamic variables. I have pasted the code below. Is this the proper way of using dynamic variables? Thanks, Tommy ...
4
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
12
by: scott | last post by:
Is there a way to create dynamic variables when looping through a recordset? For example below, after the 1st loop I'd have myVarA1 and myVarB1, after 2nd loop, I'd get myVarA2 and myVarB2. CODE...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
2
by: JWL | last post by:
Hi I need to create a bunch of sites with slightly dynamic CSS. Basically, all the image paths in the CSS need to be dynamic, depending on the values of certain ASP variables. I can think of...
3
by: Mark S. | last post by:
As I understand it, C# doesn't offer dynamic variable names. Below is my attempted workaround. Is what I'm doing possible? FYI, I already read all the "why in the world do you need dynamic...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.