Question

C++ program: The aim of this homework assignment is to practice writing hierarchy of classes. Design...

C++ program: The aim of this homework assignment is to practice writing hierarchy of classes. Design a hierarchy of Files. You have two different kinds of Files, Text File and an Image File. The files are identified by their name and type, which is identified by either txt or gif extension. An Image file has dimensions of pixel rows and pixel columns. Each pixel has a color depth that can be represented by number of bits. (8 bits is one byte). A Text file is a plain ASCII file that contains characters. Each character is represented by 8 bits. You should provide a function getSize that returns size of a file. Size of a file is measured in bytes. The size of an Image file is calculated by finding how many pixels are needed to store a file multiplied by number of bits to color a single bit divided by 8. The size of the Text file is calculated by counting number of ASCII characters stored in a file. Provide a function to display properties of a File. Properties include: type, name, size, dimensions and color depth for an Image File, characters for a Text File. Provide functions to return type and names of files. Provide function to retrieve character count for a Text file. Provide functions to retrieve dimensions and color depth of an Image File. Write .h and .cpp files for File, Image, and Text classes.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

File.cpp

#include "File.h"

// Constructor of File that takes File name and type as arguments
File::File(string type, string name) {
   this->type = type;
   this->name = name;
}
//returns the type.
string File::getType() {
   return type;
}

//returns the name of file.
string File::getName() {
   return name;
}

File.h

#ifndef __FILE_H__
#define __FILE_H__

#include <iostream>
#include <string>

using namespace std;

// class declration of File class
class File{
   public:
       // constructor.
       File(string, string);

       string name; // Name of file
       string type; // Type of file

       // virtual functions to be defined in inheriting classes.
       virtual size_t getSize(){}
       virtual void displayProperties(){}

       // returns type and name of file.
       string getType();
       string getName();
};

#endif

Image.cpp

#include "Image.h"

// Constructor for image class with call to constructor
// of parent class.
Image::Image(string name, int r, int c): File(name,"image") {
  
   row = r;
   col = c;

    int** colorDepth = new int*[row];
   for(int i = 0; i < row; ++i)
            colorDepth[i] = new int[col];

}

// returns the size of Image. Please check if the logic is right or not.
size_t Image::getSize() {
   return row*col;
}

// Function to display the properties of file.
void Image::displayProperties() {
   cout<<"File Name : "<<getName()<<endl;
   cout<<"File Type : "<<getType()<<endl;
   cout<<"File Size : "<<getSize()<<endl;
   cout<<"Image File Dimensions : "<<getHeight()<<" x "<<getWidth()<<endl;
   cout<<"Color Depth\n"<<endl;
   for(int i = 0;i<row; i++) {
       for(int j = 0;j<col; j++) {
           cout<<colorDepth[i][j]<<" ";
       }
       cout<<endl;
   }

}

// returns the height of image.
int Image::getHeight() {
   return row;
}

// returns the width of image.
int Image::getWidth() {
   return col;
}

// retunr the image.

char** Image::getColorDepth() {
   return colorDepth;
}

Image.h

#ifndef __IMAGE_H__
#define __IMAGE_H__

#include "File.h"

// Image File class declaration.
class Image: public File {
   public:
       // dimensions of image.
       int row, col;
       // color depth of each pixel
       char **colorDepth;

       // constructor of image
       Image(string name, int r, int c);
       // returns the size of file.
       size_t getSize();
       // display properties of Image file.
       void displayProperties();
       // returns the height of file.
       int getHeight();
       // returns the width of file.
       int getWidth();
       // return sthe color depth.
       char **getColorDepth();

};

#endif

Text.cpp

#include "Text.h"

// constructor definition giving constructor call to its parent.
Text::Text(string name, string contents):File(name,"text") {
   characters = contents;
}

// returns size of file.
size_t Text::getSize() {
   return characters.size();
}

// display Properties of file.
void Text::displayProperties() {

   cout<<"File Name : "<<getName()<<endl;
   cout<<"File Type : "<<getType()<<endl;
   cout<<"File Size : "<<getSize()<<endl;
   cout<<"File contents : "<<getCharacters()<<endl;
}

// returns contents of file.
string Text::getCharacters() {
   return characters;
}

Text.h

#ifndef __TEXT_H__
#define __TEXT_H__

#include "File.h"

// class declaration with public inheritance of File class.
class Text: public File {

   public:
       // file contents.
       string characters;

       Text(string name, string contents);
       size_t getSize();
       void displayProperties();
       string getCharacters();

};

#endif

Add a comment
Know the answer?
Add Answer to:
C++ program: The aim of this homework assignment is to practice writing hierarchy of classes. Design...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

  • MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...

    MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class...

    Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class Account and derived class Savings-Account. Base class Account should include one data member of type double to represent the account balance. The class should provide a constructor that receives an initial baiance and uses it to initialize the data member. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

  • This assignment tests your ability to write C programs that handle keyboard input, formatted console output,...

    This assignment tests your ability to write C programs that handle keyboard input, formatted console output, and input/output with text files. The program also requires that you use ctype functions to deal with the logic. In this program, you will input from the user two characters, which should both be letters where the second letter > the first letter. You will then input a file character-by-character and output those letters that fall between the two characters (inclusively) to an output...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer...

    write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer class (Computer) that describes a computer object. A computer has the following properties: Amount of Ram in GB (examples: 8, 16), defaults to 8. Hard drive size in GB (examples: 500, 1000), defaults to 500. Speed in GHz (examples: 1.6, 2.4), defaults to 1.6. Type (laptop, desktop), defaults to "desktop" Provide the following functions for the class: A default constructor (constructor with no parameters)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT