LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2009, 04:25 PM   #1
action_owl
Member
 
Registered: Jan 2009
Location: 127.0.0.1
Distribution: Fedora, CentOS, NetBSD
Posts: 115

Rep: Reputation: 17
What are these two lines of C++ code doing?


I just started a new C++ class and we had an in-class exercise where the teacher handed out parts of the solution to the problem before we were done.
So I never fully understood what was going on in two parts, and I really would like to.


Code:
class SavingsAccount
{
public:
   SavingsAccount( double b )
   {
      savingsBalance = ( b >= 0.0 ? b : 0.0 );
   }

   SavingsAccount &calculateMonthlyInterest();
   static void modifyInterestRate( double );
   void printBalance() const;

private:
  double savingsBalance;
  static double annualInterstRate;

};
The lines in question are:
Code:
SavingsAccount &calculateMonthlyInterest();
//The class... then the address of calculateMonthlyInterest()?
Code:
void printBalance() const;
//what is the const doing after printBalance()?
Any explanations are very appreciated, in the hand out nearly every line of the code is commented except the ones I didn't understand.
This is not homework
 
Old 12-13-2009, 04:44 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The whole section of code you quoted is the definition of a class.

The lines you asked about are declarations of functions that are members of the class.

Code:
SavingsAccount &calculateMonthlyInterest();
The & means the function returns a reference to a SavingsAccount object, rather than returning a SavingsAccount object by value.

Code:
void printBalance() const;
First you should understand that a function that is a non static member of a class takes a pointer to an object of that class as an implied extra parameter. So printBalance() appears to take zero parameters, but it really takes one parameter.

If you declared just
Code:
void printBalance();
then the implied extra parameter is
SavingsAccount* this;

The const you asked about in
Code:
void printBalance() const;
causes that implied extra parameter to be declared as
SavingsAccount const* this;

Quote:
Originally Posted by action_owl View Post
This is not homework
I don't care, because it looks like reasonable question for you to ask here even if it were part of a homework assignment. If some detail within an assignment confuses you, ask about it. That is not the same as asking people to do the assignment for you.

Last edited by johnsfine; 12-13-2009 at 04:55 PM.
 
1 members found this post helpful.
Old 12-13-2009, 04:48 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Code:
SavingsAccount &calculateMonthlyInterest();
This is a function returning a reference.. the last line is very likely "return *this;", similar to the assignment operator, the iostreams stream insertions, and the Named Parameter idiom.

Code:
void printBalance() const;
The "const" marks that this member function should not modify the context object, and the compiler enforces this assertion. Const-correctness is one of the most valuable features the C++ compiler offers.

Last edited by tuxdev; 12-13-2009 at 04:50 PM.
 
Old 12-13-2009, 05:09 PM   #4
action_owl
Member
 
Registered: Jan 2009
Location: 127.0.0.1
Distribution: Fedora, CentOS, NetBSD
Posts: 115

Original Poster
Rep: Reputation: 17
wow, thank you johnsfine and tuxdev,

You helped clear this up for me.

It appears that his C++ II class is going to be a lot harder than C ++ I, but that means I'm going to learn more as well.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
three lines of code ... Filipe Programming 1 10-23-2008 12:38 PM
Need quick C++ code review(150 lines),please sidney.harrell Programming 7 06-09-2007 08:48 AM
[Discussion] Ten Lines of Code or Less taylor_venable Programming 17 12-12-2006 05:55 AM
adding code lines Qwo Linux From Scratch 17 05-24-2006 02:58 AM
How to count how many lines of code my project is? The_Nerd Programming 4 08-30-2004 08:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration