Answer to Question #251326 in C++ for Zeeshan

Question #251326
Write a definition of a Counter class having one private data member count of integer
type. This class has following functions
à ƒ ƒ ¯ ‚ · void inc_count( ); // will increment the value of count by 1
à ƒ ƒ ¯ ‚ · int get_count ( ); // will return the value of count
this class has two contructor
à ƒ ƒ ¯ ‚ · Counter( ); // that initialize count by 0
à ƒ ƒ ¯ ‚ · Counter (int i); // that initialize the count by i
Create two objects of Counter class. Write a cout statement in constructor and then
check whether that statement appear when two object are created. Then increment
object 1 3 times and increment object 2 4 times and display their count values.
1
Expert's answer
2021-10-14T07:55:49-0400
 #include<iostream>
 using namespace std;
 class Counter{
 	private:
 	int count;
 	public:
 		Counter(){
 			count = 0;
 			cout<<"Counter object created:\n";
		 }
		 
		Counter(int i){
		cout<<"Counter object created:\n";
			count = i;
		}
		
		void inc_count( ){
			count++;
		}
		
		 int get_count(){
		 	return count;
		 }
 };
 
 int main(){
 	Counter c;
 	Counter c2(2);
 	Counter c3(7);
 	for(int i=0; i<3; i++){
 		c2.inc_count();
	 }
	 cout<<"The value after the increment:  "<<c2.get_count()<<endl;
	 
	 for(int i=0; i<4; i++){
 		c3.inc_count();
	 }
	 cout<<"The value after the increment:  "<<c3.get_count()<<endl;
 	
 }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS