63.6k views
5 votes
A class named Triplet1 has three integer private data members named first, second, and third. Write the definition of this class as follows:

a. Given that the default initial value of each of these data members is 1, write the class constructor(s) that will be used to initialize the objects of this class with zero, one, two, or three arguments.
b. Write the member functions getFirst( ), getSecond( ), and getThird( ) that returns the values of member variables first, second, and third respectively.
c. Overload the operator pre-increment ++ for the class Triplet1.
pre-increment ++ adds 1 to the value of each member variable of its operand and returns its reference.

1 Answer

3 votes

Final answer:

The Triplet1 class is defined with private data members and member functions for accessing their values. The ++ operator is overloaded to increment all three data members.

Step-by-step explanation:

The following is the definition of the Triplet1 class:

class Triplet1 {
private:
int first;
int second;
int third;
public:
Triplet1(int f=1, int s=1, int t=1) : first(f), second(s), third(t) {}
int getFirst() const { return first; }
int getSecond() const { return second; }
int getThird() const { return third; }
Triplet1& operator++() {
++first;
++second;
++third;
return *this;
}
};

User Ali Kahoot
by
7.9k points