-
May 28th, 2017, 08:03 AM
#1
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()
}
-
May 28th, 2017, 08:47 AM
#2
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.
-
May 28th, 2017, 09:10 AM
#3
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 28th, 2017, 09:32 AM
#4
Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods
Originally Posted by AvDav
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 28th, 2017, 10:19 AM
#5
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.
-
May 28th, 2017, 10:59 PM
#6
Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods
Thank you 2kaud for the feedback and insight.
-
May 28th, 2017, 11:00 PM
#7
Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods
Thanks AvDav. I'll check it out.
-
May 28th, 2017, 11:20 PM
#8
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?
-
May 29th, 2017, 01:25 AM
#9
Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods
Originally Posted by meshc++
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.
-
May 29th, 2017, 02:57 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|