7 Replies - 476 Views - Last Post: 27 May 2011 - 09:02 AM Rate Topic: -----

#1 Miday8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-March 11

Beginner OOP trouble

Posted 25 May 2011 - 06:07 AM

I have to create a class month and display an array of months, but I don't seem to get it right :(. Could you please tell me what I'm doing wrong

//main
#include <iostream>
#include <iostream>
#include "Month.h"
#include "Day.h"

using namespace std;
#define NR_M 12

int main()
{
    Month **month = new Month *[NR_M];
    month[0] = new Month( "Jannuary", 31) ;
    month[1] = new Month( "February", 28) ;
    month[2] = new Month( "March ", 31);
    month[3] = new Month( "April ", 30);
    month[4] = new Month( "May ", 31);
    month[5] = new Month( "June ", 30) ;
    month[6] = new Month( "July", 31) ;
    month[7] = new Month( "August ", 31);
    month[8] = new Month( "September", 30) ;
    month[9] = new Month( "October", 31) ;
    month[10] = new Month( "November ", 30);
    month[11] = new Month( "December ", 31);

    cout << endl<< "__________Display Months_________"<<endl<<endl;
    
    for (int i=0; i<= NR_M; i++){
        month[i]->display();
    }
    cout<<endl;

    return 0;
}






#ifndef MONTH_H
#define MONTH_H


class Month
{
    private:
        char *name;
        int nrday;

    public:
        Month(char*, int = 0);
        ~Month();
        Month(const Month& );
        void display();
        char *getMonth();
};

#endif // MONTH_H




#include "Month.h"
#include <string.h>
#include <iostream>

using namespace std;


Month::Month(char * called, int nr)
{
    //ctor
    this->nrday = nr;

    if(called!=NULL){
        this->name= new char[strlen(called)];
        strcpy(this->name, called);
    }else{
        this->name = NULL;
    }

    cout << "Month Constructor "<< this << endl;
    }


Month::~Month()
{
    //dtor
    cout << "Month Destructor "<< this << endl;
    if (name != NULL){
        delete[] this->name;
    }
}

Month::Month(const Month& m)
{
    //copy ctor
    this->nrday = m.nrday;
    if(m.name != NULL){
        this->name=new char[strlen(m.name)];
        strcpy(this->name, m.name);
    }else{
        this->name=NULL;
    }
    cout<<"Month copy constructor";
}

void Month::display(){

    if(name != NULL){
        cout<<"Month "<< this->name;
    }
    cout<<" has "<<this->nrday;
    cout<<endl;
}

char * Month::getMonth(){
    return name;

}




Is This A Good Question/Topic? 0
  • +

Replies To: Beginner OOP trouble

#2 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Beginner OOP trouble

Posted 25 May 2011 - 06:12 AM

an array of NR_M months would be declared like this
Month *month = new Month [NR_M]


what you wrote would be an array of NR_M pointers to a month. Is this really what you need?

This post has been edited by muballitmitte: 25 May 2011 - 06:14 AM

Was This Post Helpful? 1
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: Beginner OOP trouble

Posted 25 May 2011 - 06:12 AM

I think you may be going about it the wrong way or I'm misunderstanding what you really want since you didn't give much explanation.

I think it would be better to create a class Month that hold's all the months and their days instead of a separate month class for each month.
Was This Post Helpful? 1
  • +
  • -

#4 Miday8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-March 11

Re: Beginner OOP trouble

Posted 25 May 2011 - 06:22 AM

View Postcodeprada, on 25 May 2011 - 06:12 AM, said:

I think you may be going about it the wrong way or I'm misunderstanding what you really want since you didn't give much explanation.

Well this is what I should be doing:

Create clasS Month add some attributes for class.
Write a program that:
· Defines : constructor, copy constructor, destructor and display function
· Creates an array of months and displays it
· Write function that finds the months with a given number of days

Am I doing this the wrong way?

View Postmuballitmitte, on 25 May 2011 - 06:12 AM, said:

an array of NR_M months would be declared like this
Month *month = new Month [NR_M]


what you wrote would be an array of NR_M pointers to a month. Is this really what you need?

I changed that but I get nasty errors which I have no idea how to fix


\main.cpp||In function `int main()':|
|12|error: no matching function for call to `Month::Month()'|
\Month.h|14|note: candidates are: Month::Month(const Month&)|
main.cpp|16|error: no match for 'operator=' in '*(month + 24u) = (((Month*)operator new(8u)), (<anonymous>->Month::Month(((char*)((const char*)"April ")), 30), <anonymous>))'|
Month.h|6|note: candidates are: Month& Month::operator=(const Month&)|

||=== Build finished: 14 errors, 0 warnings ===|

This post has been edited by Miday8: 25 May 2011 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: Beginner OOP trouble

Posted 25 May 2011 - 06:43 AM

Yes I'm afraid you've taken a different approach. You see the way you're doing it in order to accomplish this, Write function that finds the months with a given number of days, you would have to pass every Month object to that function which is not feasible.

I'll give you tips on how to do this.

Notice this -> Creates an array of months and displays it. Since the months don't change you can hardcode the months to the array. You can also do that for the days of the months but depending on if it's a leap year or not you would either return 28 + 1 as the days in February or just 28. The array of the days in the months will be a parallel array.

Example
string * persons = new string[3];
int * age = new int[3];

persons[0] = "codeprada"; //persons is parallel with age
age[0] = 20;



This should get you on the right track.
Was This Post Helpful? 1
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3097
  • View blog
  • Posts: 10,883
  • Joined: 08-August 08

Re: Beginner OOP trouble

Posted 25 May 2011 - 07:48 AM

Because I don't like pointers or C strings, and I think vectors are generally better than arrays:
using namespace std;
#include <iostream>
#include <string>
#include <vector>

class Month
{
private:
	string name;
	int nrday;
	
public:
	Month(string m, int n = 0);
	void display();
	string getMonth();
	void changeMonth(string m, int n);
};

void Month::display(){
	cout<<"Month "<< name << " has "<<this->nrday << " days." << endl;
}

string Month::getMonth(){
	return name;	
}

Month::Month(string MonthName, int Days)
{
	this->nrday = Days;
	this->name= MonthName;
}

void Month::changeMonth(string MonthName, int Days)
{
	this->nrday = Days;
	this->name= MonthName;

}

int main () {
	int Num[12] = {31,28,31,30,31,30,31,31,30,31,30,31}, i = 0;
	string Names[12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
	Month MyYear(Names[i],Num[i]);
	vector<Month>TheYear;
	for(i = 0; i < 12; i++) {
		MyYear.changeMonth(Names[i],Num[i]);
		TheYear.push_back( MyYear);
	}
	
	for (i = 0; i < TheYear.size(); i++) {
		TheYear[i].display();
	}
	return 0;
}


This is probably not how your teacher wants it done, but hopefully it will give you some ideas.
Was This Post Helpful? 2
  • +
  • -

#7 Miday8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-March 11

Re: Beginner OOP trouble

Posted 25 May 2011 - 08:19 AM

View PostCTphpnwb, on 25 May 2011 - 07:48 AM, said:

Because I don't like pointers or C strings, and I think vectors are generally better than arrays:

[/code]
This is probably not how your teacher wants it done, but hopefully it will give you some ideas.


Well she likes char arrays for some unknown reason, so I can't use strings, but Thank you for the idea :)
Was This Post Helpful? 0
  • +
  • -

#8 msdnguide  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 17-May 11

Re: Beginner OOP trouble

Posted 27 May 2011 - 09:02 AM

i<= NR_M is wrong

size of your arrays is NR_M so the index will run from 0 to NR_M -1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1