Solved

How to explain ?

Posted on 2003-10-29
4
287 Views
Last Modified: 2010-04-02
How to explain:
Cat::Cat( int Age):itsAge(Age){}

and

Template <class T>
Class Array
{ public:
   …// constructors
 …// operators
        Array& operator= (const array&);
        T& operator [] (int offset) { return pType[offset];}
      Private: T *pType;
0
Comment
Question by:wanggang22
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9648266
=========
 How to explain:
Cat::Cat( int Age):itsAge(Age){}
==========

Explanation

The above code illustrates the definition of a Constructor of a Class called Cat.
The Constructor Initialiser List is used to initialse the class member variable  itsAge with the parameter -Age- to the constructor

-----------------------------------------------------


=============================================

Template <class T>
Class Array
{ public:
   …// constructors
 …// operators
        Array& operator= (const array&);
        T& operator [] (int offset) { return pType[offset];}
      Private: T *pType;

=============================================

Explanation

Class Array is a Template Class which is used to have a class which would let u create Array kind of object for any type [including user defined types]
It overloads certain operators like =, []

Operator = is overloaded to copy an existing Array kind of object into a new Array object

Operator [] is overloaded to assign a value to one of the locations of the Array Object
0
 
LVL 1

Expert Comment

by:beavis_shenzhen
ID: 9648281
I dont know where your troule lies.

"Cat::Cat( int Age):itsAge(Age){}"

the codes define a constructor of  class cat.
the following would be more understandable

Cat::Cat(int Age)
{
   ..default constructor of itsaAge
    ...constructor of class of tsAge with parameter age.
    ...assign the new value to itsAge.
}
the differences betweent the two forms of constructors is that the latter call a default constructor of itsAge and cosume a  little longer cpu time.
0
 
LVL 2

Accepted Solution

by:
MirkoLMV earned 125 total points
ID: 9660981
Cat::Cat( int Age):itsAge(Age){}
This line is a definition of a constructor for the Cat class (I have met such classes in SAMS Teach Yourself C++ in 24 hours).
The empty body of the function shows that it does not do something particular, just create the new instance object of the Cat class.
Between the parameters list and the opening curly brace of the body there is an initialization list. Such lists are preceded by the colon Operator and comma-separated (not in such a case, since there is a single member). The itsAge(Age) construction has the same effect as itsAge = Age; more conventional statement: to the itsAge data member it is assigned the actual value of the Age formal parameter. The initialization list is credited with better performance than conventional initializations within the constructor body because it is less employed the activation record.

Template <class T>
Class Array
{ public:
   // constructors
 // operators
        Array& operator= (const array&);
        T& operator [] (int offset) { return pType[offset];}
      Private: T *pType;
This piece of code has a major problem: a part of the class is missing, and it could have some importace.
It is a template class, designed tp handle arrays with elements of any type. Arrays are unidimensional here. T is the type of the array elements (integer, character, structure, maybe even objects) and the system avoids the writing of similar code just for type-issued differences.
Array& operator= (const array&); line is actually the header of a special function called overloaded operator. Such overloads are done for providing a syntax similar to conventional operators for functions which are performing ressemblant actions on user-defined types (for example addition of an integer to an integer member of a structure).
In this case, it is the assignment of a whole array to another. The parameter is the source (reference to an array) and the keyword const is employed to limit the access just to read-only, thus avoiding any change of the actual parameter's value. The destination is the array to store the value returned by the operator. Here it seems to be error: the name of the unique parameter should start with a capital and thus avoid type mismatches when copying the values.
The line  T& operator [] (int offset) { return pType[offset];}  is an overloaded array subscript operator (indexing operator, usded for accessing the array for reading or writing). It is an in-line function, because repeating of a single statement affects less the performance than the conventional function call with the subsequent activation record.

Good luck !
 
0
 
LVL 2

Expert Comment

by:MirkoLMV
ID: 9675233
I am glad that I was of a help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question