Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

How to explain ?

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
wanggang22
Asked:
wanggang22
  • 2
1 Solution
 
Sys_ProgCommented:
=========
 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
 
beavis_shenzhenCommented:
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
 
MirkoLMVCommented:
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
 
MirkoLMVCommented:
I am glad that I was of a help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now