Question

An array of class objects is similar to an array of some other data type. To...

An array of class objects is similar to an array of some other data type. To create an array of Points, we write
Point parray [4];
To access the object at position i of the array, we write
parray [i]
and to call a method on that object method, we write
parray [i]. methodName (arg1 , arg2 , ...) ;
To initialize an array of objects whose values are known at compile time, we can write
Point parray [4] = { Point (0 ,1) , Point (1 ,2) , Point (3 ,5) , Point (8 ,13) };
We can also allocate an array of objects dynamically using the new operator (this implicitly calls the default constructor of each new Point):
Point * parray = new Point [4]; 1.2 Static members and variables
Static data members of a class are also known as “class variables,” because there is only one unique value for all the objects of that class. Their content is not different from one object of this class to another.
For example, it may be used for a variable within a class that can contain a counter with the number of objects of the class that are currently allocated, as in the following example:
1. # include <iostream >
2. using namespace std ;
3. class CDummy
4. {
5. public :
6. static int n;
7. CDummy () { ++n; }
8. ~CDummy() { --n; }
9. };
10. int CDummy ::n = 0;
11. int main ()
12. {
13. CDummy a;
14. CDummy b [5];
15. CDummy * c = new CDummy ;
16. cout << a .n << "\n" ; // prints out 7
17. delete c;
18. cout<< CDummy ::n << "\n"; //
19. return 0;
20. }
In fact, static members have the same properties as global variables, but they can only be referenced via the class: either in class methods, via a class instance (someObject.staticVariable, or via the className::variable construct.
Because these variables are global, if we were to initialize them in a header file we could end up with that initialization being compiled multiple times (once per time we include the header). To avoid this, we only include a static member’s “prototype” (its declaration) in the class declaration, but not its definition (its initialization). This is why line 10 above is necessary, and why if we were to provide a header file for CDummy, we would still need to put line 10 in a separate .cpp file. If you get linker errors saying a static int is undefined, check to see whether you’ve included a line like line 10 in a .cpp file.
Classes can also have static member functions – that is, member functions which are associated with the class but do not operate on a particular class instance. Such member functions may not access non-static data members. For instance, we might replace CDummy above with the following class definition:
prints
out 6

class CDummy
{
private :
static int n;
public :
CDummy () { ++n; } ~CDummy() { --n; }
static int getN () { return n;} };
getN could then be called as c –> getN() or CDummy::getN().

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Yes you can invoke the static methods like shown in the description.

Below is the sample code to show you how to invoke:

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

#include<iostream >
using namespace std;

class CDummy
{
private :
    static int n;
public :
    CDummy ()
    {
        ++n;
    }

    ~CDummy()
    {
        --n;
    }

    static int getN ()
    {
        return n;
    }
};


int main()
{
    CDummy a;
    CDummy b[5];
    CDummy * c = new CDummy ;
    cout << CDummy::getN() << "\n" ;
    delete c;
    cout << c->getN() << "\n";
    return 0;
}


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

Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settings Help main2.cpp x 2 4 25 26int main) 27 pr


Hope this helps.

Add a comment
Know the answer?
Add Answer to:
An array of class objects is similar to an array of some other data type. To...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part...

    Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part 1: In this assignment, you are asked: Stage1:?Design and implement a class named memberType with the following requirements: An object of memberType holds the following information: • Person’s first name (string)?• Person’s last name (string)?• Member identification number (int) • Number of books purchased (int)?• Amount of money spent (double)?The class memberType has member functions that perform operations on objects of memberType. For the...

  • Write a program

    1. Write a program which have a class named as “Student”, while the data members of the class are,a. char name[10],b. int age;c. string color;d. string gender;e. double cgpa;and the member function “print()”,  is just to display these information.1. You need to create 3 objects of the “Student” class.2. Initialize one object by the default constructor having some static values.3. Initialize the second object by the parameterized constructor, while you need to pass values for initialization.4. Initialize the third object...

  • Provide short answers for the following C++ questions const - data members: - how can you...

    Provide short answers for the following C++ questions const - data members: - how can you initialize data members that are const: - member functions: - how to declare member functions that operate on a const object: - why declare them to handle const objects: inheritance: - class hierarchy - order of events when a derived-class object is instantiated and destroyed -constructor initialization list

  • 1a. Create a class named Computer - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Computer - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - an int variable named year - a string variable named model - a string variable named purpose 1c Add the following public functions: - Default constructor which sets numeric members to -1 and string members to the empty string - Destructor to print "Removing instance from memory" - A non-default constructor to set the year and...

  • JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate...

    JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate passing array reference to a method;        III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...

  • C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header ...

    C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header files given. Then make a main program using Book and Warehouse to read data from book.dat and have functions to list and find book by isbn Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should accept the same input data file and support the same list and find operations. You will change the implementation of...

  • Design a class named NumDays, to store a value that represents a number of hours and...

    Design a class named NumDays, to store a value that represents a number of hours and convert it to a number of days. For example, 24 hours would be converted to 1 day, 36 hours would be converted to 1.5 days, and 54 hours would be converted to 2.25 days. This class has two private member variables with data type double: one is named as hours; another is named as days. This class has a constructor that accepts a value...

  • Define a class DoubleStack which implements two stacks of objects of type Object using a single...

    Define a class DoubleStack which implements two stacks of objects of type Object using a single shared array, so that the push and pop operations specify which of the two stacks is involved in the operation (as described below). (a) Specify necessary instance variables and write a constructor “DoubleStack(int n)” that takes integer n and creates an empty DoubleStack object with the shared array with room for n elements (2 pt); b) Write methods "boolean push(int i, Object o)" and...

  • C++ IJU U Construct a user-defined object class named Bug that models a bug moving along...

    C++ IJU U Construct a user-defined object class named Bug that models a bug moving along a horizon- tal line. The bug moves either to the right or left. Initially, the bug moves to the right, but it can turn to change its direction. In each move, its position changes by one unit in the current direction. The Bug class will have data members position (type int) for the bug's position on the horizontal line and dir (type int) for...

  • Your will write a class named Gasket that can be used to build Gasket objects (that...

    Your will write a class named Gasket that can be used to build Gasket objects (that represent Sierpinski gaskets) that you can display graphically.  Your Gasket class is defined in the provided file Gasket.h.   DO NOT CHANGE the file Gasket.h.  The attributes and methods defined for class Gasket are described below.     ·sideLength            an int that holds the length of each side of the Gasket.  The length of the side is measured as a number of pixels ·xLocation              an int that holds the x coordinate (in pixels)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT