CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2013
    Posts
    2

    Question Class/private help with this uml?

    So I'm trying to do a homework assignment, where I read a uml about a bank program, and just create it. It's been about 2 years since I've done any c++. I used to be very quick and talented with it. But i have a horrible retention rate.

    Here is the UML.
    Name:  umlcrap_zps586f637e.png
Views: 930
Size:  68.0 KB


    I'm completely oblivious with these UMLs, never learned much about them. But I am giving it my best shot. So while working on Bank, the top one, i've come up with this so far.
    Code:
    #include <iostream>
     #include <string>
     using namespace std;
     class bank
     { string name; 
    int routingNum; 
    private: 
    int createaccount(string, int);
     };
     int bank::createaccount(string name, int routingNum) 
    { cout <<"--------Registration--------\n";
     cout << "Please enter your name\n";
     cin >> name;
     cout << "Please enter your Routing Number\n";
     cin >> routingNum;
     return (name,routingNum);
     cin.get();
     cin.get(); 
    } 
    int main() 
    { bank bankk;
     bankk.createaccount(name,routingNum);
     cout<<"Thank you"<<bank.name();
     return 0; }
    I'm assuming that's what the uml wants. However I can't seem to access that private class. Idk why. I declared it as an object in the main. Can anyone assist please? And if possible point out as many errors as possible, need tons of criticism to get back into my c++ mind.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Class/private help with this uml?

    It's not the class that's private - its the method. Members of a class are private by default which means they can only be accessed by other class members. If you need to access them outside of the class definition they need to be declared public. In your class just change private to public.

    Also createaccount is defined to return an int - but you have

    Code:
    return (name,routingNum);
    which is invalid. createaccount needs to update the class variables and then return some number so doesn't need any parameters.

    Also main cannot access directly the private member name. You need a class method to return this data.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class bank {
    	string	name; 
    	int	routingNum;
    
    public: 
    	int	createaccount();
    	string	getName();
     };
    
    int bank::createaccount() 
    {
    	cout <<"--------Registration--------\n";
    	cout << "Please enter your name: ";
    	cin >> name;
    	cout << "Please enter your Routing Number: ";
    	cin >> routingNum;
    	return 0;
    }
    
    string bank::getName()
    {
    	return name;
    }
    
    
    int main() 
    {
    bank	bankk;
    
    	bankk.createaccount();
    	cout << "Thank you " << bankk.getName();
    	return 0;
    }

  3. #3
    Join Date
    Feb 2013
    Posts
    2

    Re: Class/private help with this uml?

    Quote Originally Posted by 2kaud View Post
    It's not the class that's private - its the method. Members of a class are private by default which means they can only be accessed by other class members. If you need to access them outside of the class definition they need to be declared public. In your class just change private to public.

    Also createaccount is defined to return an int - but you have

    Code:
    return (name,routingNum);
    which is invalid. createaccount needs to update the class variables and then return some number so doesn't need any parameters.

    Also main cannot access directly the private member name. You need a class method to return this data.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class bank {
    	string	name; 
    	int	routingNum;
    
    public: 
    	int	createaccount();
    	string	getName();
     };
    
    int bank::createaccount() 
    {
    	cout <<"--------Registration--------\n";
    	cout << "Please enter your name: ";
    	cin >> name;
    	cout << "Please enter your Routing Number: ";
    	cin >> routingNum;
    	return 0;
    }
    
    string bank::getName()
    {
    	return name;
    }
    
    
    int main() 
    {
    bank	bankk;
    
    	bankk.createaccount();
    	cout << "Thank you " << bankk.getName();
    	return 0;
    }
    But doesn't that minus sign on the uml mean it has to be private?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Class/private help with this uml?

    I think in your UML diagram for Bank that createAccount is mis-represented and it should be public. It makes no sense to have createAccount private as that means you won't be able to create an account outside of the class and derived classes won't be able to use it either. If createAccount is meant to be used only by derived classes from bank then it could be made protected instead of public - but in terms of your program to test the class it still needs to be public. I would check with your tutor about this.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Class/private help with this uml?

    also:
    if you get homework assignments it's a good idea to make sure you know what you're supposed to be doing.
    since you don't know what UML is. why didn't you ask your tutor/teacher ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured