C++ header files My header file looks like this: #ifndef PPMIMAGE_H_ #define PPM
ID: 3879417 • Letter: C
Question
C++ header files
My header file looks like this:
#ifndef PPMIMAGE_H_
#define PPMIMAGE_H_
class PpmImage{
public:
void readImage(char* data);
void setRows(int row){
rows = row;
}
void setCols(int col){
cols = col;
}
void setDataArray(int size){
data = new char[size];
}
int getRows(){
return rows;
}
int getCols(){
return cols;
}
char* getDataArray(){
return data;
}
private:
int rows;
int cols;
char* data;
};
#endif
Now, I need to implement readImage() function in my .cpp file which now only contains the main method:
#include "ppmImage.h"
using namespace std;
int main(int argc, char** argv) {
PpmImage image;
image.setRows(-1);
image.setCols(-1);
int rows = image.getRows();
int cols = image .getCols();
}
how to implement the readImage() function in my .cpp?
Explanation / Answer
PpmImage.h file:
#ifndef PPMIMAGE_H_
#define PPMIMAGE_H_
class PpmImage{
private:
int rows;
int cols;
char* data;
public:
void readImage(char* data);
void setRows(int row){
rows = row;
}
void setCols(int col){
cols = col;
}
void setDataArray(int size){
data = new char[size];
}
int getRows(){
return rows;
}
int getCols(){
return cols;
}
char* getDataArray(){
return data;
}
};
#endif
=============================================
PpmImage.cpp file:
save below code in ppmImage.cpp file
#include <iostream>
#include "ppmImage.h"
using namespace std;
void PpmImage::readImage(char* data)
{
//Here you can write the code
}
==========================================
client code: main.cpp
#include <iostream>
#include "ppmImage.h"
using namespace std;
int main(int argc, char** argv) {
PpmImage image;
image.setRows(-1);
image.setCols(-1);
int rows = image.getRows();
int cols = image .getCols();
//using image you can call readImage function as per your requirement.
return 0;
}
finally compile and run the main.cpp file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.