LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2004, 11:07 PM   #1
toovato
Member
 
Registered: Jul 2003
Location: Ft Lauderdale, FL
Distribution: debian
Posts: 48

Rep: Reputation: 15
c/c++ questions


got two questions:

1) What does 3[x]++ do?

2) new to oo - have an assignment to write an object with the following conditions:
The door can have two states (open or closed)
- Create methods for finding the state of the door (open or closed)
- Create methods for opening and closing the door
State any assumptions and additional features you have made on the Door class.

Thanks in advance,
 
Old 02-26-2004, 11:25 PM   #2
ElementNine
Member
 
Registered: Sep 2003
Distribution: Red Hat 9 or Gentoo 1.4 whatever I can get to work first
Posts: 105

Rep: Reputation: 15
Hmm i think that would cause an error. var[x]++ would return a value one higher that the one pointed to by x so if var[x] = 3 then var[x]++ would return 4

you wanna create a door class and probably have a variable "doorstatus" and two methods "void setDoorState(boolean)" "bool getDoorState(void)" setDoorState take a boolean parameter and sets the variable "doorstatus" to true or false getDoorState would just return doorstatus
 
Old 02-26-2004, 11:29 PM   #3
toovato
Member
 
Registered: Jul 2003
Location: Ft Lauderdale, FL
Distribution: debian
Posts: 48

Original Poster
Rep: Reputation: 15
thanks for you help - could you provide a code snippet?
 
Old 02-26-2004, 11:34 PM   #4
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
3[x]++;
just increments the value in x[3];
 
Old 02-26-2004, 11:44 PM   #5
ElementNine
Member
 
Registered: Sep 2003
Distribution: Red Hat 9 or Gentoo 1.4 whatever I can get to work first
Posts: 105

Rep: Reputation: 15
Code:
class Door {
       public:
               Door(); //constructor
               void setDoorStatus(bool);
               bool getDoorStatus();
        private:
               bool doorStatus;
};

Door::Door(){
        doorStatus = true; //true for open false for closed
}

void Door::setDoorStatus(bool ds){
         doorStatus = ds;
}

bool Door::getDoorStatus(){
         return doorStatus;
}
I think that should work, someone let me know if i messed up but im pretty sure thats right

edit sorry that smiley is actually a colon and a D but it turned into a smiley
 
Old 02-26-2004, 11:48 PM   #6
ElementNine
Member
 
Registered: Sep 2003
Distribution: Red Hat 9 or Gentoo 1.4 whatever I can get to work first
Posts: 105

Rep: Reputation: 15
eshwar are you sure that doesnt cause an error it looks like he's trying to increment a value inside an array named 3 which is an error but if he meant x[3]++ then you and i are right it would add one to the value of x arrays 4th value
 
Old 02-26-2004, 11:59 PM   #7
toovato
Member
 
Registered: Jul 2003
Location: Ft Lauderdale, FL
Distribution: debian
Posts: 48

Original Poster
Rep: Reputation: 15
you have been a great help - thanks a million
 
Old 02-27-2004, 12:09 AM   #8
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
Mr. Element Nine
both are same.
3[x] is same as x[3].
When it comes to 3[x]++, 3[x] is evaluated first because [] has highest precedence than ++.
So there is no problem .
3[x]++ is same as x[3]++.
what do u say. check it.
 
Old 02-27-2004, 12:34 AM   #9
ElementNine
Member
 
Registered: Sep 2003
Distribution: Red Hat 9 or Gentoo 1.4 whatever I can get to work first
Posts: 105

Rep: Reputation: 15
uh unless everything ive learned has been wrong, that looks like an array correct? arrays structure is var[index] and variables cant start with a number
 
Old 02-27-2004, 12:47 AM   #10
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
yah what ever you said is correct. you might be thinking that X[3] as a variable name, No its not. i think you know very well variable names cant have a charcter '[' like this. valid names contain only alphabets, _ , 0....9.
and one more rule is your variable name shouldnt be started with a number.
actually X[3] is not a variable name. X is the name of array. Its the base address of array( we cant change X value). no other names are there when we declare an array as below
int X[3];

ex:
datatype X[10];
when you use X[3], it is expanded as *((datatype *)X+3)
did u get my point?
ask me if u have any doubts regarding this.
bye,
Eshwar.
 
Old 02-27-2004, 12:56 AM   #11
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
Hi thats an equivalent expression. I am not sure whether that is expanded like that or not by compiler.
but its equivalent is absolutely correct.
 
Old 02-27-2004, 01:20 AM   #12
toovato
Member
 
Registered: Jul 2003
Location: Ft Lauderdale, FL
Distribution: debian
Posts: 48

Original Poster
Rep: Reputation: 15
so which is right?
 
Old 02-27-2004, 01:28 AM   #13
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
Both are correct.
 
Old 02-27-2004, 02:24 AM   #14
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
Quote:
Originally posted by ElementNine
uh unless everything ive learned has been wrong, that looks like an array correct? arrays structure is var[index] and variables cant start with a number
They are expanded to *(argv + 1) ( for example ). The thing you do not seem to realise from that is that `argv' is in fact a number, as are all other variables. They are memory references.
 
Old 02-27-2004, 09:16 AM   #15
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Rep: Reputation: 30
Quote:
Originally posted by ElementNine
var[x]++ would return a value one higher that the one pointed to by x so if var[x] = 3 then var[x]++ would return 4
Actually, if var[x] == 3 then var[x]++ will return 3. It will increment it after the operation. If var[x] == 3 then ++var[x] will return 4.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
basic questions on hostname and domain name + related postfix questions Moebius Linux - Newbie 7 09-04-2007 11:50 AM
Solaris - Questions! Questions! Questions! qs_tahmeed Solaris / OpenSolaris 2 07-16-2005 05:27 AM
window manager questions and/or theme questions t3gah Linux - Software 2 02-27-2005 04:16 PM
Some questions... CryptDragoon Linux From Scratch 2 02-04-2004 08:28 PM
few questions? pudhiyavan Linux - General 2 10-03-2003 07:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration