473,397 Members | 1,972 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,397 software developers and data experts.

Calling for a char from a different class.

Main.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "Rooms.h"
  3. using namespace std;
  4.  
  5. void main()
  6. {
  7.  
  8.     Rooms town;
  9.     town.setName((char)"town");
  10.     cout << town.getName();
  11. }
Rooms.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "Rooms.h"
  3.  
  4. Rooms::Rooms()
  5. {
  6. }
  7.  
  8. Rooms::~Rooms()
  9. {
  10. }
  11.  
  12. void Rooms::setName(char input)
  13. {
  14.     name =(char) input;
  15. }
  16.  
  17. char Rooms::getName()
  18. {
  19.     return (char)"name";
  20. }
Rooms.h:
Expand|Select|Wrap|Line Numbers
  1. #ifndef ROOMS_H
  2. #define ROOMS_H
  3.  
  4. class Rooms
  5. {
  6. public:
  7.     Rooms();
  8.     ~Rooms();
  9.     void setName(char input);
  10.     char getName();
  11.     char name;
  12. };
  13.  
  14. #endif
am i too tired to find a problem or am i just coding it worng? i run it and i get a funny symbol not name like i thought i should.
May 21 '07 #1
7 1358
gpraghuram
1,275 Expert 1GB
HI,
I think problem is with the function
Expand|Select|Wrap|Line Numbers
  1. char Rooms::getName()
  2. {
  3.     return (char)"name";
  4. }
  5.  
First u are returning wrongle,instead of returning the variable name u are returning the string "name" typecasting it to char.
chage the code like this
Expand|Select|Wrap|Line Numbers
  1. char Rooms::getName()
  2. {
  3.     return name;
  4. }
  5.  
Raghuram
May 21 '07 #2
svlsr2000
181 Expert 100+
Main.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "Rooms.h"
  3. using namespace std;
  4.  
  5. void main()
  6. {
  7.  
  8.     Rooms town;
  9.     town.setName((char)"town");
  10.     cout << town.getName();
  11. }
Rooms.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "Rooms.h"
  3.  
  4. Rooms::Rooms()
  5. {
  6. }
  7.  
  8. Rooms::~Rooms()
  9. {
  10. }
  11.  
  12. void Rooms::setName(char input)
  13. {
  14.     name =(char) input;
  15. }
  16.  
  17. char Rooms::getName()
  18. {
  19.     return (char)"name";
  20. }
Rooms.h:
Expand|Select|Wrap|Line Numbers
  1. #ifndef ROOMS_H
  2. #define ROOMS_H
  3.  
  4. class Rooms
  5. {
  6. public:
  7.     Rooms();
  8.     ~Rooms();
  9.     void setName(char input);
  10.     char getName();
  11.     char name;
  12. };
  13.  
  14. #endif
am i too tired to find a problem or am i just coding it worng? i run it and i get a funny symbol not name like i thought i should.
"Name" would give a pointer to a character and your trying to cast it to character. Change your setname prototype to setName(char *Input) and use town.setName("town");. Try to find out the difference between two. :)
May 21 '07 #3
svlsr2000
181 Expert 100+
"Name" would give a pointer to a character and your trying to cast it to character. Change your setname prototype to setName(char *Input) and use town.setName("town");. Try to find out the difference between two. :)
Sorry i was bit late. :)
May 21 '07 #4
code removed as per posting guidelines

Hope this helps

Expand|Select|Wrap|Line Numbers
  1. char* Rooms::getName()
  2. {
  3.     return name;
  4. }
May 21 '07 #5
gpraghuram
1,275 Expert 1GB
Hi,
You got it right.

Thanks
Raghuram
May 21 '07 #6
thanks guys that helped.
May 21 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
Not a good choice:
Expand|Select|Wrap|Line Numbers
  1. char* Rooms::getName()
  2. {
  3.     return name;
  4. }
  5.  
You have broken encapsulation by returning a pointer to your name. That means someone outside the class can use the pointer to change the name without needing to use the class member functions. Worse, if the name is a literal, the program will crash if an attempt is made to change the name.

You should return a const char* from this method. Ans since the method itself does not change meember data, the method shoulc be const:
Expand|Select|Wrap|Line Numbers
  1. const char* Rooms::getName() const
  2. {
  3.     return name;
  4. }
  5.  
May 21 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
4
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
2
by: Andre | last post by:
Hi, I have a problem with calling a managed C++ function from C#. The function in C++ looks like Class::Func(char* Text) this function is compiled to an DLL and this DLL is referenced in my...
11
by: j23 | last post by:
I have library (static) testlib.cpp: #include <stdarg.h> void xxx(...) { char buf; va_list args; va_start(args, buf); va_end(args); }
1
by: Nick Bishop | last post by:
I have a problem where I call a method in a C++ class with a pointer which is a static member in that class. When I use a debugger, I see the pointer having a certain value, but when I step into...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
12
by: Peter Cranz | last post by:
hello, I've got the following problem: I have a construct similar like this: namespace A { class X {
1
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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,...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.