Help Me with this code. i JUST installed code::Blocks and am not familiar with the errors , I wanted to know what is going wrong with this?

#include <iostream>
using namespace std;

int f();
class Date{
        int d,m,y;
        static Date default_date;

        public:
        void set_default(int dd,int mm,int yy);
        Date(int,int,int);
        Date& addyear(int);
        int show_date(){return d;}
        int show_month(){return m;}
        int show_year(){return y;}
        };

Date::Date(int dd=0,int mm=0,int yy=0)
        {
            d=dd? dd:default_date.d;
            m=mm? mm:default_date.m;
            y=yy? yy:default_date.y;
        }
Date& Date::addyear(int n)
{
    y +=n;
    return *this;
}

void Date::set_default(int dd,int mm,int yy)
{
    Date::default_date.d=dd;
    Date::default_date.m=mm;
    Date::default_date.y=yy;

}

int main()
{
Date::set_default(9,7,2000);
Date S(3,7,1991);
cout<<"Date"<<S.show_date()<<"-"<<S.show_month()<<"-"<<S.show_year()<<"\n";
S.addyear(18);
cout<<"Date"<<S.show_date()<<"-"<<S.show_month()<<"-"<<S.show_year();
}

You should post your compiler errors, but what I see right away:

Date::set_default(9,7,2000);
Date S(3,7,1991);

You call the function set_default before you created the class. And you call the memberfunction the wrong way (Date:: instead of S.)
So change the two lines to:

Date S(3,7,1991);
	S.set_default(9,7,2000);

[edit]
Also your set_default function calls the members-vars the wrong way

1) To make the call Date::set_default(), needs the set_default() to be a static member function.
2) It is not enough to declare a static member variable inside the class declaration, you also need to define and optionally initialize it.

class Date
{
    // a non-const default date
    static Date default_date;

    // a const default date
    static const Date default_date_const;

    int d,m,y;        

public:
    static void set_default(int dd,int mm,int yy);
    Date(int,int,int);
	
    <snip>
}; // class Date ends here

// the non-const default date defined & initialized here
Date Date::default_date(9,7,2000);
// the const default date defined & initialized here
const Date Date::default_date_const(9,7,2000);

int main()
{
    // Date::set_default() is now a static member function,
    //  so the following call works
    Date::set_default(9,7,2000);
    Date S(3,7,1991);
    cout<<"Date"<<S.show_date()<<"-"<<S.show_month()<<"-"<<S.show_year()<<"\n";
    S.addyear(18);
    cout<<"Date"<<S.show_date()<<"-"<<S.show_month()<<"-"<<S.show_year();
    return 0;
}
commented: thanks for the correction +5

@ mitrmkar

Good call. Misfire in my brain

@sky diploma: disregard my post and use mitrmkar's

D:\Documents and Settings\Administrator\My Documents\Trys\Implementing Date.cpp|29|error: cannot declare member function `static void Date::set_default(int, int, int)' to have static linkage|
||=== Build finished: 1 errors, 0 warnings ===|

That is the error that i got I followed Mitrmkar

void Date::set_default(int dd,int mm,int yy)

{

Date::default_date.d=dd;

Date::default_date.m=mm;

Date::default_date.y=yy;

}

This is the code.

Seems that a static function has to be defined in the class itself and not outside the class.

That is not true, you really can define the static member function outside the class. Maybe you've applied the 'static' keyword also to the definition of the static member function.

Anyhow, for the sake of clarity, the following setup should compile without a glitch.

your main .cpp file

#include "date.h"
int main()
{
    Date::set_default(9,7,2000);
    Date S(3,7,1991);
    S.addyear(18);

    return 0;
}

your date.cpp file

#include "date.h"
Date Date::default_date(9,7,2000);
const Date Date::default_date_const(9,7,2000);

Date::Date(int dd=0,int mm=0,int yy=0)
{
	d=dd? dd:default_date.d;
	m=mm? mm:default_date.m;
	y=yy? yy:default_date.y;
}

Date& Date::addyear(int n)
{
    y +=n;
    return *this;
}

void Date::set_default(int dd,int mm,int yy)
{
    Date::default_date.d=dd;
    Date::default_date.m=mm;
    Date::default_date.y=yy;
}

your date.h file

#ifndef DATE_H_DEFINED
#define DATE_H_DEFINED

class Date
{
	static Date default_date;
	static const Date default_date_const;
	int d,m,y;        
public:		
	static void set_default(int dd,int mm,int yy);
	Date(int,int,int);
	Date& addyear(int);
	int show_date(){return d;}
	int show_month(){return m;}
	int show_year(){return y;}
};
#endif // #ifndef DATE_H_DEFINED

Oh okay i completely understand .Thanks for the Help Mitrmkar :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.