Re: c[++] pointer question
From: Alf P. Steinbach (alfps_at_start.no)
Date: 03/31/04
- Next message: Leor Zolman: "Re: A is a friend of B, and C a subclass of A. Is C a friend of A?"
- Previous message: Steven T. Hatton: "Re: using namespace"
- In reply to: antonis akis: "c[++] pointer question"
- Next in thread: John Harrison: "Re: c[++] pointer question"
- Reply: John Harrison: "Re: c[++] pointer question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 31 Mar 2004 00:42:01 GMT
* yaryantua@yahoo.gr (antonis akis) schriebt:
> I am new to c[++] language and I have a trouble understanding
> pointers.
A pointer is a variable that contains a memory address.
> I copy the following code from a tutorial that I read
The tutorial seems to be very very old because the code is
pre-standard.
I recommend buying an introductory book such as "You can do it"
by Francis Glassbarrow or, if you already know a bit of programming,
"Accelerated C++" by Koenig and Moe.
> and my
> question is why the variables "line_of_text" and "input_line" have to
> be declared as pointers?
They don't have to. In modern C++ the most natural data type would
be std::string. But using just the "raw" facilities directly available
in the language (which this code doesn't do anyway) one would need to
either use fixed-length arrays, or use a function that reserves memory
of specified size and returns a pointer to it. In the latter case the
pointer would of course be stored in a pointer variable. Which you could
then use to access the reserved chunk of memory, as in this code.
> I would like also to grasp the mechanics of it.
> ----
>
> #include <iostream.h>
This header file does not exist in standard C++, but it was common
in pre-standard (pre-1998) C++ implementations.
In current C++ write
#include <iostream>
That includes the text from the "iostream" file, which among other
things contains declarations of std::cout used for console output.
> class box {
Starts the declaration of a class called "box".
> int length;
> int width;
> char *line_of_text;
The above are member variables. That means that every variable of
type "box" will contain three member variables called respectively
"length", "width" and "line_of_text". Since the class declaration
was started using the word "class" instead of "struct" these member
variables are private by default, meaning they are accessible to
code in the class but not to code that doesn't belong to the class.
The point of that is to give the class code full control so it can
always assume that no other code has made any changes to the member
variables.
In modern C++ the last one would be
std::string line_of_text;
> public:
This introduces a section of public declarations, meaning those things
(whatever) will be accessible to code that doesn't belong to the class.
> box(char *input_line); //Constructor
A constructor is a special member function that is meant to initialize
the member variables. You cannot create a class-type object without a
constructor being called, or from another point of view, you cannot
call a constructor without also creating a class-type object. The point
of that is to strongly couple allocation with initialization so that
once you got a variable of type "box" you can be sure that it's initialized.
In modern C++ the argument would either be
box( char const * input_line );
where 'const' says that the constructor cannot modify the actual argument,
thus allowing const arguments, or else it would be
box( std::string const& input_line );
for using the std::string standard library string type.
> void set(int new_length, int new_width);
A simple member function. When you got yourself a "box" object, say "b",
you can call this member function _on_ the object using the syntax
"b.set( 1, 2 );". References to member variables in the code in the
function will then refer to the object "b" that you called it on.
> int get_area(void);
Ditto. The word "void" is C'ism that has no meaning here in C++.
> };
This closing brace and semicolon ends the class declaration. The
semicolon is important. It is a common beginner's mistake to forget
the semicolon, causing the compiler to apparently find an error somewhere
much further in the code, perhaps in some other file.
> box::box(char *input_line) //Constructor implementation
> {
> length = 8;
> width = 8;
> line_of_text = input_line;
> }
This constructor code initializes the member variables of this instance
of "box".
In modern C++ the initialization is better done via an initializer list,
box::box( char* input_line )
: length( 8 ), width( 8 ), line_of_text( input_line )
{}
> // This method will set a box size to the two input parameters
> void box::set(int new_length, int new_width)
> {
> length = new_length;
> width = new_width;
> }
Changes the values of the member variables the instance that "set"
is called on.
> // This method will calculate and return the area of a box instance
> int box::get_area(void)
As mentioned, "void" is C'ism, just remove it.
> {
> cout << line_of_text << "= ";
Generally not a good idea to let a member function to more than one
thing, in this case both output and returning a result.
> return (length * width);
> }
> void main()
"main" is a function that is automatically called when you run the
program.
It's usually the top level of control in a program.
"main" must have return type "int", even if your compiler erronously
let you get away with "void".
> {
> box small("small box "), //Three boxes to work with
> medium("medium box "),
> large("large box ");
> small.set(5, 7);
> large.set(15, 20);
> cout << "The area of the ";
> cout << small.get_area() << "\n";
> cout << "The area of the ";
> cout << medium.get_area() << "\n";
> cout << "The area of the ";
> cout << large.get_area() << "\n";
> }
-- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail?
- Next message: Leor Zolman: "Re: A is a friend of B, and C a subclass of A. Is C a friend of A?"
- Previous message: Steven T. Hatton: "Re: using namespace"
- In reply to: antonis akis: "c[++] pointer question"
- Next in thread: John Harrison: "Re: c[++] pointer question"
- Reply: John Harrison: "Re: c[++] pointer question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|