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()
}
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.
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/
Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods
Quote:
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++.
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.
Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods
Thank you 2kaud for the feedback and insight.
Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods
Thanks AvDav. I'll check it out.
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?
Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods
Quote:
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.
Re: C++ OOP-Revision Question on Class, Data Members, and Member Methods
Thanks Wolle. I came to the right learning place :thumb: