Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to explain ?

Posted on 2003-10-29
4
Medium Priority
?
306 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 375 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

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!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.

670 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