Need help on these few questions: 3a) create a class that contains a normal memb
ID: 3843559 • Letter: N
Question
Need help on these few questions:
3a) create a class that contains a normal member function and a static member function.
3b) Create an object of that class and, inside a for loop, call both functions multiple times.
3c) Compile the program and see if you can check the assembler to see where the arguments are being passed and verify that this pointer is only passed in one situation.
// word5.h
#ifndef _WORD5_H
#define _WORD5_H
class Word {
private:
char *buf;
static int wordCount; public:
Word(char *s);
int getCount(); };
#endif
// word5.cpp
#include <string.h>
#include "word5.h"
int Word::wordCount = 0;
Word::Word(char *s) { buf = strdup(s);
wordCount++; }
int Word::getCount() {
return wordCount; }
Explanation / Answer
Please find the code below.
The logic is simple.
Create 2 functions - Non-Static and Static.
Non-Static functions will only have the size of the string passed to the current instance of the class.
Since Static functions is not a member of class, it will add up the
sizes of the string passed on to the each instance of the class.
from this we can infer that, this pointer will only affect the Non-Static member function
since it will not track the previous instances of the class.
CODE:
word5.h
#include <iostream>
#include <string>
#ifndef _WORD5_H
#define _WORD5_H
// Word class defined
class Word {
private:
// declare string buf
std::string buf;
static int totalCount;
int wordCount;
public:
Word(std::string s);
int getCount();
// getWordCount is static function that returns total count.
static int getWordCount(){
std::cout << "Inside Static function" << std::endl;
return totalCount;
}
};
#endif
word5.cpp
#include "word5.h"
// static variable initialized to zero.
int Word::totalCount = 0;
// constructor
Word::Word(std::string s) {
buf = s;
wordCount = buf.size();
totalCount += wordCount;
}
// Non-Static member function that returns wordCount.
int Word::getCount() {
std::cout << "Inside Non-Static function" << std::endl;
return wordCount;
}
int main(){
std::cout << "WordCount program" << std::endl;
// call the Word class 5 times with Hello as the input to constructor.
// display the output of Non-Static and Static member functions.
// Non-Static member functions will only display the size of the current input.
// Static functions will add the count of all the previous instances of the same class.
for(int i=0;i<5;i++){
Word a("Hello");
std::cout << a.getCount() << std::endl;
std::cout << Word::getWordCount() << std::endl;
}
}
OUTPUT:
$ g++ word5.cpp
$ ./a.out
WordCount program
Inside Non-Static function
5
Inside Static function
5
Inside Non-Static function
5
Inside Static function
10
Inside Non-Static function
5
Inside Static function
15
Inside Non-Static function
5
Inside Static function
20
Inside Non-Static function
5
Inside Static function
25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.