Solved

How to explain ?

Posted on 2003-10-29
4
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

751 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