473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

intrestring puzzle

The following source file does not compile.
#include <cstdio>
class X {
public:
X() { i = 100; }
private:
int i;
};
int main()
{
X x;
printf("%d\n", x.i);
}

Add one line (having one semicolon) to the definition of X that in
turn makes the file compilable. itsValue shud b private.
Jul 22 '05 #1
10 1502
* us********@yahoo.com (dada) schriebt:
The following source file does not compile.
Add one line (having one semicolon) to the definition of X that in
turn makes the file compilable. itsValue shud b private.


Yes, it's friday, but don't drink and post at the same time.

Jul 22 '05 #2
On 27 Feb 2004 10:19:59 -0800, us********@yahoo.com (dada) wrote:
The following source file does not compile.
#include <cstdio>
class X {
public:
X() { i = 100; }
private:
int i;
};
int main()
{
X x;
printf("%d\n", x.i);
}

Add one line (having one semicolon) to the definition of X that in
turn makes the file compilable. itsValue shud b private.


class X {
public:
X() { i = 100; }
private:
public: int x; // added this
int i;
};

Problem is, I have no idea what you mean by
itsValue shud b private.
Is that the name of a variable? If so, where did it come from, and if not,
values aren't public or private, so what are you talking about? And could
you use English, please? ;-)
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
dada escribió:
Add one line (having one semicolon) to the definition of X that in
turn makes the file compilable. itsValue shud b private.


friend int main ();

Regards.
Jul 22 '05 #4
"Julián Albo" <JU********@terra.es> wrote in message news:40***************@terra.es..."dada escribió: "> Add one line (having one semicolon) to the definition of X that in
"> turn makes the file compilable. itsValue shud b private. "friend int main ();


also std::printf.

Jonathan
Jul 22 '05 #5
"Jonathan Turkanis" <te******@kangaroologic.com> wrote
"Julián Albo" <JU********@terra.es> wrote in message

news:40***************@terra.es...
"dada escribió:

"> Add one line (having one semicolon) to the definition of X that in
"> turn makes the file compilable. itsValue shud b private.

"friend int main ();


also std::printf.


Making printf() a friend wouldn't help because it's not the one trying to access
x.i. main() is accessing x.i and passing its value to printf(). :-)

Claudio Puviani

Jul 22 '05 #6

"Claudio Puviani" <pu*****@hotmail.com> wrote in message
news:Sh********************@news4.srv.hcvlny.cv.ne t...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote
"Julián Albo" <JU********@terra.es> wrote in message news:40***************@terra.es...
"dada escribió:

"> Add one line (having one semicolon) to the definition of X that in"> turn makes the file compilable. itsValue shud b private.

"friend int main ();


also std::printf.


Making printf() a friend wouldn't help because it's not the one

trying to access x.i. main() is accessing x.i and passing its value to printf(). :-)


I wasn't suggesting making it a friend. I was just pointing out that
namespace qualification was missing.

Jonathan
Jul 22 '05 #7
dada wrote:
The following source file does not compile.
#include <cstdio>
class X {
public:
X() { i = 100; }
private:
int i;
};
int main()
{
X x;
printf("%d\n", x.i);
}

Add one line (having one semicolon) to the definition of X that in
turn makes the file compilable. itsValue shud b private.


Firdays [sp] are bad days to post issues that can
be resolved by consulting the FAQ.
http://www.parashift.com/c++-faq-lit...d-objects.html
http://www.parashift.com/c++-faq-lite/friends.html
http://www.parashift.com/c++-faq-lite/newbie.html

Here is the reason. You have declared the variable 'i'
as private (having private accessibility). "private"
means that only class member functions have access
to those items. Any function outside of the class
cannot see nor access a private item.

The compiler is complaining that the main() function
is trying to access a private data member.

If you want a private member to be displayed, you
either have to create a member function to do that:
class X
{
// as above
public:
void print(void)
{ printf("%d\n", i);}
void print_cpp_style(void)
{ cout << i << endl;}
void print_better_style(ostream& out)
{ out << i; }
};

int main(void)
{
X junk;
junk.print();
junk.print_cpp_style();
junk.print_better_style(cout);
cout << endl;
return 0;
}
or you will have to place the member variable
into a public section:
class X
{
public:
int i;
};

Also, due yourself a favor and look up "initialization
list" in your favorite text book or help file.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #8
yes u r rite.but its possible to access a private member directly by
using pointers.like for eg
X x
X *ptr = &x;
int *info;
info=(int*)ptr;
info will be able to print out i.but i want to
access the private data by including just 1 line in the program
to the definiton of X with only 1 semicolon. this is the requirement
of the puzzle.
u shud not change the main function
class X {
public:
X() { i = 100; }
private:
int i;
};
int main()
{
X x;
printf("%d\n", x.i);
}


Thomas Matthews <Th****************************@sbcglobal.net> wrote in message news:<hU*******************@newssvr16.news.prodigy .com>... dada wrote:
The following source file does not compile.
#include <cstdio>
class X {
public:
X() { i = 100; }
private:
int i;
};
int main()
{
X x;
printf("%d\n", x.i);
}

Add one line (having one semicolon) to the definition of X that in
turn makes the file compilable. itsValue shud b private.


Firdays [sp] are bad days to post issues that can
be resolved by consulting the FAQ.
http://www.parashift.com/c++-faq-lit...d-objects.html
http://www.parashift.com/c++-faq-lite/friends.html
http://www.parashift.com/c++-faq-lite/newbie.html

Here is the reason. You have declared the variable 'i'
as private (having private accessibility). "private"
means that only class member functions have access
to those items. Any function outside of the class
cannot see nor access a private item.

The compiler is complaining that the main() function
is trying to access a private data member.

If you want a private member to be displayed, you
either have to create a member function to do that:
class X
{
// as above
public:
void print(void)
{ printf("%d\n", i);}
void print_cpp_style(void)
{ cout << i << endl;}
void print_better_style(ostream& out)
{ out << i; }
};

int main(void)
{
X junk;
junk.print();
junk.print_cpp_style();
junk.print_better_style(cout);
cout << endl;
return 0;
}
or you will have to place the member variable
into a public section:
class X
{
public:
int i;
};

Also, due yourself a favor and look up "initialization
list" in your favorite text book or help file.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #9
In message <f7**************************@posting.google.com >, dada
<us********@yahoo.com> writes
yes u r rite.but its possible to access a private member directly by
using pointers.like for eg
X x
X *ptr = &x;
int *info;
info=(int*)ptr;
info will be able to print out i.


#include <iostream>
#include <ostream>
class X
{
public:
X() : i(100) {}
virtual ~X() {}
private:
int i;
};

int main()
{
X x;
X *ptr = &x;
int *info;
info=(int*)ptr;
std::cout << *info << std::endl;
return 0;
}

Strange. I tried that here but it printed 4202796. Any idea why that
might be?

--
Richard Herring
Jul 22 '05 #10

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:Dw**************@baesystems.com...
In message <f7**************************@posting.google.com >, dada
<us********@yahoo.com> writes
yes u r rite.but its possible to access a private member directly by
using pointers.like for eg
X x
X *ptr = &x;
int *info;
info=(int*)ptr;
info will be able to print out i.
#include <iostream>
#include <ostream>
class X
{
public:
X() : i(100) {}
virtual ~X() {}
private:
int i;
};

int main()
{
X x;
X *ptr = &x;
int *info;
info=(int*)ptr;


What do you think this assignment is supposed to do?
All you've done with the cast, is lie to the compiler.
Saying it doesn't make it so.
std::cout << *info << std::endl;
return 0;
}

Strange. I tried that here but it printed 4202796. Any idea why that
might be?


Nobody can explain undefined behavior. It's undefined.
(or at least implementation-defined, in which case you
need to read your documentation to find out).

-Mike
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Developwebsites | last post by:
Hi all, I've made a sliding puzzle game in shockwave which works just fine, except I dont know how to have it solve itself. the URL is: http://members.aol.com/rglukov/games/selfsolve.htm ...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
5
by: dmf1207 | last post by:
Hi All! I'm new to javascript and need a little help with a simple puzzle im trying to design. I have a 600x100 pixel picture that I have sliced into 6 100x100 rectangles making a table of of 6...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.