Jump to content

A little bit of help is all i need

biggiepk

Hi guys, 

Can somebody help me how to do this? Or link me to some tutorial?  

thanks 

 

Create a class Book with private attributes  book Title, book Author, numOfPages and constructors: zero argument, constructor with parameters, getters and seters for each attribute.

 

Link to comment
Share on other sites

Link to post
Share on other sites

I would suggest you to go online for this. This forum might not be a good page, in my opinion. But someone should be able to help. Or I think you should try to find it over the internet, or perhaps look it up on YouTube.

Link to comment
Share on other sites

Link to post
Share on other sites

Oh god please don't tell me it's java...

I only remember that i used netbeans and i could generate the getters and setters but that's it...

If you want my attention, quote meh! D: or just stick an @samcool55 in your post :3

Spying on everyone to fight against terrorism is like shooting a mosquito with a cannon

Link to comment
Share on other sites

Link to post
Share on other sites

We need three files for this, I'm not sure what language you are using but this example is in C++.

I've not used the exact names and properties you mentioned, this should be enough to get you started but I wont do it for you.

 

Any questions, ask away.

 

 

Main.cpp (The main section of your program, here we will call the getter/setter/constructor functions for your class)

#include <iostream>
#include "Book.h"

int main() {
    // Construct a book with parameters
    Book Novel("The Chronicals", 5000);
  
    // Print out the size of the book
    std::cout << "Book Length: " << Novel.getSize() << std::endl;
  
    // Construct a book with no parameters
    Book Notes();
  
    // Set size of "Notes" book.
    Notes.setSize(1000);
  
    // Verify the new size of the "Notes" book.
    std::cout << "Book Length: " << Notes.getSize() << std::endl;
  
    return 0;
}

 

Book.h (The book class declaration)

#ifndef BOOK_H
#define BOOK_H

class Book {
public:
    // Provide a constructor with parameters
    Book(const char* bookName, unsigned int size);
  
    // Provide a blank constructor
    Book();

    // Provide a destructor
	~Book();
	
    // Declare Getter/Setter functions
    unsigned int getSize();
    void setSize(unsigned int bookSize);

private:
    unsigned int size;
    std::string name;
};


#endif

 

Book.cpp (The definition of the functions declared in Book.h)

#include <iostream>
#include <string>

#include "Book.h"

// Class constructor with values
Book::Book(const char* bookName, unsigned int bookSize) {
	name = bookName;
	size = bookSize;
	std::cout << "Book constructed with name: " << name << std::endl;
};

// Class blank constructor
Book::Book(){
    name = "unNamed"
    size = 0;
    std::cout << "Book constructed with name: " << name << std::endl;
};

// Class destructor
Book::~Book() {
	std::cout << "Book destructed with name: " << name << std::endl;
}

// Getter for Book Size
unsigned int Book::getSize() {
	return this->size;
}

// Setter for Book Size
void Book::setSize(unsigned int bookSize) {
	this->size = bookSize;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×