473,791 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1537
* us********@yaho o.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********@yaho o.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********@ter ra.es> wrote in message news:40******** *******@terra.e s..."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******@kanga roologic.com> wrote
"Julián Albo" <JU********@ter ra.es> wrote in message

news:40******** *******@terra.e s...
"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*****@hotmai l.com> wrote in message
news:Sh******** ************@ne ws4.srv.hcvlny. cv.net...
"Jonathan Turkanis" <te******@kanga roologic.com> wrote
"Julián Albo" <JU********@ter ra.es> wrote in message news:40******** *******@terra.e s...
"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_st yle(ostream& out)
{ out << i; }
};

int main(void)
{
X junk;
junk.print();
junk.print_cpp_ style();
junk.print_bett er_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 "initializa tion
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.l earn.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******* ************@ne wssvr16.news.pr odigy.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_st yle(ostream& out)
{ out << i; }
};

int main(void)
{
X junk;
junk.print();
junk.print_cpp_ style();
junk.print_bett er_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 "initializa tion
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.l earn.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********@yah oo.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

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

Similar topics

1
6110
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 There are lots of these on the net, made in java, flash, C++, javascript, etc. and as shareware, but none seem to be solving the puzzle by
1
13109
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 the other....how i can fix this the program look like this import java.util.ArrayList; import java.util.Random;
0
2027
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 other....how i can fix this this run the random words for the program import javax.swing.JOptionPane; import java.util.ArrayList; import java.util.Random; public class CrossWordPuzzleTester {
5
4473
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 is to solve jigsaw puzzles using the computer. The jigsaw puzzle here is a square of dimension d (a puzzle with d^2 pieces) and the jigsaw pieces (all same dimensions) are of dimensions H x W (Which means the pieces have ‘H’ rows of ‘W’...
3
3209
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 to bottom (S), or (iv) bottom to up (N). The program will print the starting place and the direction of each word. Limitations The number of words to be searched can be at most 100, the size of the puzzle N can be minimum 5 maximum 20....
6
2578
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 and http://norvig.com/ltd/test/n-puzzle.lisp ) I have used OO Python for the above program and would like comments on my approach as I am just starting with OOP.
2
2705
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 -- AND NOW FOR A WORD (an IF blog):
4
20006
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 get the tiles in order, 1 through 15, from left to right, top to bottom, by just sliding tiles into the empty square. In this configuration, the goal would be to get the 14 and 15 to switch places, without affecting any of the other squares. Your...
5
4844
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 columns and 1 row. I wanted to make a "swap puzzle" where the user clicks on one image then the other and they swap. At first load i want to have the puzzle in a scrambled order and then when they complete it I want to alert the user with how many clicks...
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.