Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,230 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,301 people online right now. Registration is fast and FREE... Join Now!




declare const in classes

 
Reply to this topicStart new topic

declare const in classes

doubts
10 Dec, 2007 - 04:05 AM
Post #1

New D.I.C Head
*

Joined: 1 Nov, 2007
Posts: 17


My Contributions
Hi all...I want declare a constant variable in class...After that, i declare 3 objects of that class...Now how will i give the values to these variables...i am using the following code....
CODE
#include<iostream.h>    
#include<conio.h>

class test
{
private:
     int i;
    const int j;
public:
    test()
    {
      i=12;
      j=12;
    }
    
    void display(void)
    {
        cout<<i<<"\n";
        cout<<j<<"\n";
    }
};
void main()
{
    test t1,t2,t3;
    t1.display();
    t2.display();
    t3.display();
}


This post has been edited by doubts: 10 Dec, 2007 - 04:06 AM
User is offlineProfile CardPM
+Quote Post

Bench
RE: Declare Const In Classes
10 Dec, 2007 - 04:35 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Use an initialiser list after the constructor's signature

CODE
class test
{
private:
     int i;
    const int j;
public:
    test() : i(12), j(12)
    {
    }
    
    void display(void)
    {
        cout<<i<<"\n";
        cout<<j<<"\n";
    }
};

User is offlineProfile CardPM
+Quote Post

doubts
RE: Declare Const In Classes
10 Dec, 2007 - 07:58 PM
Post #3

New D.I.C Head
*

Joined: 1 Nov, 2007
Posts: 17


My Contributions
thanku...it works....but i also want to know that if i have declared 3 objects of class.....so how to give 3 different values of i and j by invoking these objects...

This post has been edited by doubts: 10 Dec, 2007 - 08:05 PM
User is offlineProfile CardPM
+Quote Post

skaoth
RE: Declare Const In Classes
10 Dec, 2007 - 08:48 PM
Post #4

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 357



Thanked: 12 times
Dream Kudos: 100
My Contributions
What you can do is define an overloaded constructor
CODE

class test
{
private:
    int i;
    const int j;
public:
    test() : i(12), j(12)
    {
    }
    test (int val1, int val2) : i(val1), j(val2)
    {

    }

    void display(void)
    {
        cout<<i<<"\n";
        cout<<j<<"\n";
    }
};


The usage would be something like this
CODE

int main()
{

    test myclass1(100, 200);
    test myclass2(400, 500);

    myclass1.display();
    myclass2.display();

    return 0;
}


cheers
User is offlineProfile CardPM
+Quote Post

doubts
RE: Declare Const In Classes
10 Dec, 2007 - 09:24 PM
Post #5

New D.I.C Head
*

Joined: 1 Nov, 2007
Posts: 17


My Contributions
Thanku so much....Now if i want to make the object constant rather than the variable then what should be done....
User is offlineProfile CardPM
+Quote Post

Bench
RE: Declare Const In Classes
11 Dec, 2007 - 01:38 PM
Post #6

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(doubts @ 11 Dec, 2007 - 05:24 AM) *

Thanku so much....Now if i want to make the object constant rather than the variable then what should be done....

Then use the const keyword next to the type. Do you have a book which explains these concepts? If not, I do suggest you invest in one, it will explain this kind of thing and much more
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 06:06AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month