Constructor

I have to write a program based of this header file and I can't even get the constructor right. Will someone show me how to do it? Thanks.

// File: Peg.h
/// Class Peg from Main and Savage
//
// provides a "peg" that can hold a maximium of 64 disks
// of sizes 1 to MAX_INT in diameter.
//
// CONSTRUCTOR: Peg(x)
// POSTCONDITION the Peg has been initialized to have x disks of size x to size 1
// on the peg
//
// CONSTANT functions:
//
// size_t count ();
// POSTCONDITIONS: the number of disks on the Peg is returned
//
// Disk top_Disk ();
// PRECONDITIONS: the Peg is not empty
// POSTCONDITIONS: the disk at the top of the Peg is returned
//
//
// MODIFICATION functions:
//
// void add (const Disk added_Disk);
// PRECONDITIONS: the Peg is not full AND the added_Disk is smaller than the top disk
// POSTCONDITIONS: the Peg has the disk added to the top of the peg
//
// void remove ();
// PRECONDITIONS: the Peg is not empty
// POSTCONDITIONS: the top disk is removed from the Peg
//
// FRIENDS
// operator <<
// POSTCONDITIONS: the disk on the peg are printed from the bottom to the top
//

#define MAX_DISKS 64
#include <cstdlib>
#include <cassert>
#include <iostream>

class Peg
{
public:
typedef std::size_t Disk;
Peg(std::size_t init_disks=64);
std::size_t count() const;
Disk top_Disk() const;
void add(const Disk added_Disk);
void remove ();

friend std::ostream& operator<< (std::ostream&, const Peg&);

private:
std::size_t size;
std::size_t disks[MAX_DISKS];
};
You define a constructor like any other function, except a ctor doesn't want the return type written before itself

If you want you can define it inside the class declaration like this
1
2
3
4
5
6
7
8
9
10
class Peg
{
    public:
        typedef std::size_t Disk;
        Peg(std::size_t init_disks=64)
        {
              // Your instructions here
        }
        // Etc
};


Or you can use a dedicated cpp file. The definition goes like this
1
2
3
4
5
6
#include "Header_of_the_class.h"

Class_name::Function_name(parameters)
{
    // Your instructions here
}


Note that you have to NOT write any return type. Ctors and dtors never return anything.
Since a constructor has the same name as the class Function_name and Class_name will be identical
How would I write the constructor to do what it wants me to do in the instructions?
Can someone please answer this. This program was due yesterday and I have no one to help me and no one will answer me on here.
no one will answer me on here.


It looks like maeriden already answered you yesterday.
I still don't understand.
This might help you understand the structure of writing the class a little better.
http://www.cprogramming.com/tutorial/lesson12.html
I still don't understand.
You don't understand what?
Just so you know, we try to avoid doing homework in your place here. It's bad practice.
I just don't know what to write for the constructor. That's the only thing I was asking for. It's fine, don't worry about it.
As I don't really know what the class is supposed to to, all I can do is guess that the parameter init_disks must be assigned to size
Topic archived. No new replies allowed.