C++ OOP-Revision Question on Class, Data Members, and Member Methods
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: C++ OOP-Revision Question on Class, Data Members, and Member Methods

  1. #1
    Join Date
    Nov 2016
    Posts
    7

    C++ OOP-Revision Question on Class, Data Members, and Member Methods

    Having acquired very basic skills in C++, I've started learning Object Oriented Programming in C++ for a week now. I came across a revision question below and got stuck. I feel there are some other details the question expects me to include the coding. I'm still a beginner in as far as C++ structures are concerned, and will appreciate your guidance/assistance on this as I had no other references or examples that could guide me. For any incorrect presentations here or formatting, kindly correct me. I'm still learning. Thanks you.

    Question:
    Create a C++ program with a class Account
    Data members:
    1.accountName
    2.openBalance
    3.deposit

    Member methods:
    1.getdetails()
    2.calculatecurrentbalance()

    Code:
    <#include<iostream>
    using namespace std;
    //My first attempt in creating class, data members and member methods..
    class Account
    getdetails()
    calculatecurrentbalance()
    // I'm not sure I'm right...and do not know how to continue...I guess I need an intervention on this...
    {
          char accountName;
          double openBalance;
          double deposit;{
    {Account() {Balance=0.0;}
    getdetails()
    calculatecurrentbalance()
    
    }

  2. #2
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    696

    Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods

    My few cents here:
    I've started learning C++ with the help of "Accelerated C++" by Andrew Koenig & Barbara Moo. This is a technical forum, so it is supposed to as a specific question. Since, I don't know the details (e.g. on how the functions needed to be calculated), I can't suggest something useful. Being more practical might be of help.
    Popular opinion is the greatest lie in the world.

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

    Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods

    By default, all members/functions of a class are private. In order to access class functions etc outside of the class then those that need to be (such as the class constructor(s), getdetails() etc need to be marked as public.

    For the class member variables, accountname sounds like it would contain more than one char so a more appropriate type would be string - as type char can only contain one character.

    The syntax of the class definition is not quite right. Consider
    Code:
    #include <string>
    using namespace std;
    
    class Account
    {
    public:
    	Account() {}
    	void getdetails(string& name, double& open);
    	double calculatecurrentbalance();
    
    private:
    	string accountname;
    	double openBalance = 0.0;
    	double deposit = 0.0;
    };
    This is a basic class definition based upon post #1. The implementation of the class functions need to be provided.

    How are you learning c++? Have a look at
    http://www.learncpp.com/cpp-tutorial...d-programming/
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.1)

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

    Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods

    Quote Originally Posted by AvDav View Post
    My few cents here:
    I've started learning C++ with the help of "Accelerated C++" by Andrew Koenig & Barbara Moo. This is a technical forum, so it is supposed to as a specific question. Since, I don't know the details (e.g. on how the functions needed to be calculated), I can't suggest something useful. Being more practical might be of help.
    Note that 'Accelerated c++' was published Aug 2000 and is nearly 17 years old and way out of date. It doesn't cover the c++11 standard - never mind the newer c++14 standard. IMO I won't recommend it from which to learn modern c++.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.1)

  5. #5
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    696

    Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods

    I'd recommend, "Programming principles and practice using C++" 2nd ed. by Bjarne Stroustrup.
    This book fully covers the details of C++1x programming.
    Popular opinion is the greatest lie in the world.

  6. #6
    Join Date
    Nov 2016
    Posts
    7

    Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods

    Thank you 2kaud for the feedback and insight.

  7. #7
    Join Date
    Nov 2016
    Posts
    7

    Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods

    Thanks AvDav. I'll check it out.

  8. #8
    Join Date
    Nov 2016
    Posts
    7

    Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods

    Very well appreciated...both your clarifications, input, and reference. The question gave no further details which I too feel was necessary for a complete executable program. Shouldn't it have allowed for data input/output under main function, declarations of operations/calculations,function calls, etc? Well, being a beginner, translating my basic theories to codes is still a challenge. Or could it be that the question only focused on class, and it's members hence was not meant for execution?

  9. #9
    Join Date
    Feb 2017
    Posts
    158

    Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods

    Quote Originally Posted by meshc++ View Post
    Or could it be that the question only focused on class, and it's members hence was not meant for execution?
    It's quite probable. In OOP there's a principle called encapsulation which together with inheritance and polymorphism constitute the three major principles of OOP.

    Encapsulation means you divide a class into two separate parts, a visible outside and a hidden inside. The outside is a public interface consisting of a set of functions. Ideally this interface should be minimal, that is complete without being redundant. The inside consist of a private implementation. The advantage of encapsulation is that the implementation can be changed independently of the interface since nothing outside the class depends on it.

    My guess is that the purpose of this question was to apply the principle of encapsulation on a C++ class (although it this time just meant putting the functions in a public section and the data members in a private). This is what 2kaud shows in #3.
    Last edited by wolle; May 29th, 2017 at 01:40 AM.

  10. #10
    Join Date
    Nov 2016
    Posts
    7

    Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods

    Thanks Wolle. I came to the right learning place

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)