Please check my C++

This is a discussion on Please check my C++ within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by matsp Seeing as you need to have a car to read in the value anyways, you may ...

  1. #211
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Seeing as you need to have a car to read in the value anyways, you may just as well use the "this" pointer that is already there. Is Car::Read a static function? If not, it is being passed "this" as well as car, so if you do car.Read(... car ...) then you are effectively passing car along twice - once as this, and second as a parameter. Seems unnecessary, and there's no purpose (or advantage) to having a Car reference parameter to a function that requires to have a Car object to be called. Since "simpler is better", this gives the advantage to the model that anon is suggesting.

    --
    Mats
    Oops, i took it for granted that you'll see the title of my post...

    For this program, which would you recommend and why (advantages)?
    [vector vs dequeue vs maps]

  2. #212
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, I see now. I think the general rule is "use vector unless you are sure you need one of the others".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #213
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Friend Classes

    Here's my scenario concerning my friend class..

    in FleetMenu.cpp
    Code:
    class FleetMenu
    {
    public:
    	friend class ContractMenu;
    
    	FleetMenu();
    
    private:
    	void Pause();
    	void Clrscr();
    in ContractMenu.h
    Code:
    class ContractMenu
    {
    public:
    
    	ContractMenu();
    	void Run();
    
    private:
    
    ....
    
    private:
    ....
    
    };
    in ContractMenu.cpp
    Code:
    void ContractMenu::Run()
    {
    Clrscr();
    }
    Problem
    Code:
    : error C3861: 'Clrscr': identifier not found
    am new to friends...

  4. #214
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to:
    1. make Clrscr static [I presume it doesn't actually use any member content]
    2. call FleetMenu::Clrscr().

    By making to classes friends, you are not inheriting the behaviour, you are just letting the other class use the private member functions of the class.

    However, I'm not convinced that is the best way to form a menu - perhaps something based on tables is a better suggestion.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #215
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    You need to:
    1. make Clrscr static [I presume it doesn't actually use any member content]
    2. call FleetMenu::Clrscr().

    By making to classes friends, you are not inheriting the behaviour, you are just letting the other class use the private member functions of the class.

    However, I'm not convinced that is the best way to form a menu - perhaps something based on tables is a better suggestion.

    --
    Mats
    Code:
    class FleetMenu
    {
    public:
    	//friend class ContractMenu;
    
    	FleetMenu();
    
    private:
    	static void Pause();
    	static void Clrscr();
    Code:
    void ContractMenu::Run()
    {
    FleetMenu::Clrscr();
    }
    
    //  error C2248: 'FleetMenu::Clrscr' : cannot access private member declared in class 'FleetMenu'

  6. #216
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You STILL need to make it a friend if you want to use that solution, but you ALSO need to have it static.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #217
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    By the way, here's a post I made a while back with regards to creating a menu system - perhaps you can use it as a base:
    Menu (any default style ? )

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #218
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    perhaps something based on tables is a better suggestion.
    Mats
    What do you mean by tables?

  9. #219
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    What do you mean by tables?
    See post #217.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #220
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    See post #217.

    --
    Mats
    looks interesting, lets take for example the following
    Code:
    		case 1:	AddNewCar();			
    						Pause();
    						break;
    			
    		case 2:	UpdateFleetFile();					
    						Pause();
    						break;
    		
    		case 3:		EditFleetContract();				
    						Pause();
    						break;
    		
    		case 4:	DeleteCar();						
    						Pause();
    						break;
    1. how do i use your function // addmenu(std::string s, char c, menufunc f)... Or how do i replace the code above! ?
    2. If i have two different menus, how do i use your code?

  11. #221
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    looks interesting, lets take for example the following
    Code:
    		case 1:	AddNewCar();			
    						Pause();
    						break;
    			
    		case 2:	UpdateFleetFile();					
    						Pause();
    						break;
    		
    		case 3:		EditFleetContract();				
    						Pause();
    						break;
    		
    		case 4:	DeleteCar();						
    						Pause();
    						break;
    1. how do i use your function // addmenu(std::string s, char c, menufunc f)... Or how do i replace the code above! ?
    2. If i have two different menus, how do i use your code?
    1. If you move the Pause() to somewhere inside each of those functions, then you can easily just make those functions the "f" parameter (you'd have to add some extra parameters which you may or may not want to ignore).

    If you have two different menus, you just create two menu objects - you can even make hierarchical menus by creating a new menu in one of the functions you call as a result of a menu choice.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #222
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    See post #217.

    --
    Mats
    1. Can this be called link list?, or is it strictly table...
    2. Problem is with this my menu won't be nicely aligned within borders, it will be basic...

  13. #223
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    1. Can this be called link list?, or is it strictly table...
    2. Problem is with this my menu won't be nicely aligned within borders, it will be basic...
    Surely you can generate borders around it if you like - just count the number of items in the linked list and the lenght of the strings. You will want to replace this:
    Code:
        for(p = head; p; p = p->next) {
          p->item->print();
        }
    and put it into a function that counts the items + lengths, draws the frame and prints the items within a box - it shouldn't take that much effort.

    Yes, it's a linked list, which I call a table in the sense that it is data rather than code, but it's obviously not an array - you could probably replace the linked list stuff with a vector<> and push_back instead of linked list insertion.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #224
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Surely you can generate borders around it if you like - just count the number of items in the linked list and the lenght of the strings. You will want to replace this:
    Code:
        for(p = head; p; p = p->next) {
          p->item->print();
        }
    Can't i just modify this function
    Code:
    void menuitem::print() 
    { 
    	std::cout << choice << ": " << descr << std::endl;
    }
    Maybe something like..
    Code:
    	std::cout << "     \t\t#" << choice <<  "."  <<  descr << "            #\n" << std::endl;
    then by looping this somehow the hashes will from a border?

  15. #225
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, print shows only one menuitem - you need some way to determine how long ALL of the menu items are [or decide beforehand that everything should fit in X characters].

    Obviously, if you have a constant width for all menus, then (as discussed elsewhere), you could just modify print() to do the right thing. It is probably STILL a good idea to add a function "drawmenu" or some such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 10:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21