用C ++教自己OOP

时间:2016-11-30 12:12:15

标签: oop parameter-passing instantiation

所以我上个月一直在研究这个项目。原始代码来自本教程https://www.youtube.com/watch?v=KjHKwCZyAhQ&list=PLHm_I0tE5kKPPWXkTTtOn8fkcwEGZNETh&index=3

然而,在我继续之前,我想我会把它变成一个面向对象的程序。做而不是复制是学习的最佳方式。代码在我分割之前生成了一个bmp文件,但现在不再了。程序执行但它不会创建文件。另外,我在.cpp文件中添加了Hello World,看看它们是否被执行,看起来它们不是。我意识到复制这个我有很多代码,我认为问题出在主文件中,所以希望如果有人能够帮助我,他们可以更快地选择它!

*编辑 同样在原始代码中,他将strut作为全局变量,但不确定要在哪个文件中实现它,甚至不知道如何在OOP中创建全局内容!我会把它放在main上面的main()吗?

Output.h

#pragma once
#include "ProProcess.h" //this is just a bunch of preprocessor directives

//this program creats a single color bmp file using red, blue, and green (rgb)

class OutPut 
{
public:

OutPut(const int height, std::string file_name, int dpi, int index);
~OutPut();

//savebmp_str(std::string* file_name, const int width, const int height, int dpi, int pixels, struct RGBtype); 
//commented this out because I wasn't sure how I should pass all these values. Ultimately I used OutPut Object_Output in bmp.cpp so that these variables could be passed in there



const int Getwidth() { return width; }
const int Setwidth(const int x) { const int width =  x; }

private:
struct RGBtype  //Could be a global variable but I dont know which file to put it in
{
    int r;
    int g;
    int b;

};

const int width = 1960; //window size
const int height = 1080;
int dpi = 72;
int number_of_pixels = width*height;
int index;
const char* file_name = "Scene.bmp";
RGBtype *pixels = new RGBtype[number_of_pixels];//creates an array so   that each pixel is comprised of a mix of rgb

};

Output.cpp

#include "OutPut.h"
#include "ProProcess.h"

OutPut::OutPut(const int height, std::string file_name, int dpi, int index) 
{
 OutPut::RGBtype color;


for (int x = 0; x < height; x++) //nested for loop that draws out each pixel totalling 1920x1080 in all
{

    for (int y = 0; y < width; y++) 
    {
        index = y*height + x;
    pixels[index].r = 311;//changing the number here changes the color
    pixels[index].g = 311;
    pixels[index].b = 311;
    }

}

std::cout << "Hello World";
}

OutPut::~OutPut()
{

}

BMP.h

#pragma once
#include "ProProcess.h"
#include "OutPut.h"


struct RGBtype
{
int r;
int g;
int b;

};

class BMP
{
public:
BMP(const char *filename, int passed_width, int passed_height, int dpi, RGBtype* data);

~BMP();

private:
OutPut Object_Output(std::string* file_name, const int width, const int height, int dpi, int pixels, struct RGBtype);//this is to pass the variables declared in output.h so bmp.h and bmp.cpp can use them too. Not sure how I would even verify i am doing this properly!

//const char* savebmp_str();
int passed_width;
int passed_height;
int dpi;


RGBtype *data;

};

BMP.cpp

#include "BMP.h"
#include "ProProcess.h"
#include "OutPut.h"




BMP::BMP(const char *filename, int passed_width, int passed_height, int dpi, RGBtype *data)
{

std::cout << passed_height;
    FILE *pFile;
    int k = passed_width*passed_height;
    std::cout << "The value k is" << k;
    int s = 4 * k;
    int filesize = 54 + s; //s is a function of width and height

    double factor = 39.375;
    int m = static_cast<int>(factor);

    int ppm = dpi*m;

    unsigned char bmpfileheader[14] = { 'B','M',0,0,0,0 ,0,0,0,0, 54,0,0,0 }; //B and M are case sensitive. They make a bmp file
    unsigned char bmpinfoheader[40] = { 40,0,0,0, 0,0,0,0 ,0,0,0,0, 1,0,24,0 };// the header size 14 and 40 are part of the BMP format



    bmpfileheader[2] = (unsigned char)(filesize);
    bmpfileheader[3] = (unsigned char)(filesize >> 8);
    bmpfileheader[4] = (unsigned char)(filesize >> 16);
    bmpfileheader[5] = (unsigned char)(filesize >> 24);

    bmpinfoheader[4] = (unsigned char)(passed_width);
    bmpinfoheader[5] = (unsigned char)(passed_width >> 8);
    bmpinfoheader[6] = (unsigned char)(passed_width >> 16);
    bmpinfoheader[7] = (unsigned char)(passed_width >> 24);

    bmpinfoheader[8] = (unsigned char)(passed_height);
    bmpinfoheader[9] = (unsigned char)(passed_height >> 8);
    bmpinfoheader[10] = (unsigned char)(passed_height >> 16);
    bmpinfoheader[11] = (unsigned char)(passed_height >> 24);

    bmpinfoheader[21] = (unsigned char)(s);
    bmpinfoheader[22] = (unsigned char)(s >> 8);
    bmpinfoheader[23] = (unsigned char)(s >> 16);
    bmpinfoheader[24] = (unsigned char)(s >> 24);

    bmpinfoheader[25] = (unsigned char)(ppm);
    bmpinfoheader[26] = (unsigned char)(ppm >> 8);
    bmpinfoheader[27] = (unsigned char)(ppm >> 16);
    bmpinfoheader[28] = (unsigned char)(ppm >> 24);

    bmpinfoheader[29] = (unsigned char)(ppm);
    bmpinfoheader[30] = (unsigned char)(ppm >> 8);
    bmpinfoheader[31] = (unsigned char)(ppm >> 16);
    bmpinfoheader[32] = (unsigned char)(ppm >> 24);

    pFile = fopen(filename, "wb");

    fwrite(bmpfileheader, sizeof(char), 14, pFile);
    fwrite(bmpinfoheader, sizeof(char), 40, pFile);

    for (int i = 0; i < k; i++)
    {
        RGBtype rgb = data[i];
        double red = (data[i].r);
        double green = (data[i].g);
        double blue = (data[i].b);

        int color[3] = { (int)floor(blue), (int)floor(green), (int)floor(red) };

        fwrite(color, 1, 3, pFile);

    }
    fclose(pFile);
    std::cout << "Hello World";
}




BMP::~BMP()
{
}

的main.cpp

#include <iostream>
#include "OutPut.h"
#include "ProProcess.h"
#include "BMP.h"

int main()
{
OutPut Pixel_gen();               
BMP BMP_Format_Maker();
OutPut Object_Output();



system("Pause");
return 0;
}

0 个答案:

没有答案