How to 1 more formula in C++?

How to 1 more formula in the program? Inside the program got 1 formula, so I want add 1 more formula. If add 1 more, it be 2 formula in the program. I want to run the program but run 1 formula only.

Yes, below want is run 1 formula. If add 1 more?

functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

class finance_state
{
    double current_asset , current_liabilities;
public:
    finance_state(double ca , double cl);
    ~finance_state();

    double    CurrentRatio();
};
#endif  


functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "functions.h"

    finance_state  ::  finance_state(double ca , double cl) :
                                    current_asset(ca) ,
                                    current_liabilities(cl) 
{
}

    finance_state  ::  ~finance_state()
{
}

double    finance_state  ::  CurrentRatio()
{
    double temp = this->current_asset / this->current_liabilities;
    return temp;
}


EntryPoint.cpp
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "functions.h"

int    main   ()
{
    finance_state MyState( 500.0 , 300.0 );
    std::cout<< "my current ration is :\t"<< MyState.CurrentRatio()<<std::endl;
    return 0;
}
Which other formula do you want to include? You can might as well add another function like your CurrentRatio.
For this formula,

Gross margin = (Revenue – Cost of goods sold) / Revenue
Well you can include COGS and Revenue in your member variables and initialize them while constructing the object ( like your current_asset and current_liabilities ).

Then have another member function called GetGrossMargin as follows:
1
2
3
4
5
6
double finance_state::GetGrossMargin()
{
  if ( this->revenue > 0 )
    return (1 - this->cogs / this->revenue);
  return 0;
}


Note: I would suggest you include the > 0 condition for your liabilities also, before trying to calculate the current ratio.
Went I include the function, got error... The second function need include in the functions.cpp part rite??
Please show your entire implementation. What error do you get?
This is I did to run the program but got error. Can you explain me, why?

functions.h

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

class finance_state
{
    double current_asset , current_liabilities;
public:
    finance_state(double ca , double cl);
    ~finance_state();

    double    CurrentRatio();
};
#endif 


functions.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "functions.h"

    finance_state  ::  finance_state(double ca , double cl) :
                                    current_asset(ca) ,
                                    current_liabilities(cl) 
{
}

    finance_state  ::  ~finance_state()
{
}

double    finance_state  ::  CurrentRatio()
{
    double temp = this->current_asset / this->current_liabilities;
    return temp;
}

double finance_state::GetGrossMargin()
{
  if ( this->revenue > 0 )
    return (1 - this->cogs / this->revenue);
  return 0;
}


EntryPoint.cpp

1
2
3
4
5
6
7
8
9
#include <iostream>
#include "functions.h"

int    main   ()
{
    finance_state MyState( 500.0 , 300.0 );
    std::cout<< "my current ration is :\t"<< MyState.CurrentRatio()<<std::endl;
    return 0;
}
When you get an error, please post the exact text of the error.

functions.cpp
line 19 - GetGrossMargin() is not in your class declaration.
Lines 20, 21 - revenue and cogs are not members of your class.




||=== EntryPoint, Debug ===|
EntryPoint\main.cpp|line 2|functions.h: No such file or directory|

EntryPoint\main.cpp||In function `int main()':|

EntryPoint\main.cpp|line 6|error: `finance_state' was not declared in this scope|

EntryPoint\main.cpp|line 6|error: expected `;' before "MyState"|

EntryPoint\main.cpp|line 7|error: `MyState' was not declared in this scope|

EntryPoint\main.cpp|line 6|warning: unused variable 'finance_state'|

EntryPoint\main.cpp|line 7|warning: unused variable 'MyState'|

||=== Build finished: 4 errors, 2 warnings ===|
Try to make the following changes:

functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

class finance_state
{
    double current_asset , current_liabilities, cogs, revenue;
public:
    finance_state(double ca , double cl, double _cogs, double _revenue );
    ~finance_state();

    double    CurrentRatio();
    double    GetGrossMargin();
};
#endif  


functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "functions.h"

    finance_state  ::  finance_state(double ca , double cl, double _cogs, double _revenue) :
                                    current_asset(ca) ,
                                    current_liabilities(cl), cogs( _cogs ), revenue( _revenue )
{
}

    finance_state  ::  ~finance_state()
{
}

double    finance_state  ::  CurrentRatio()
{
    double temp = this->current_asset / this->current_liabilities;
    return temp;
}

double finance_state::GetGrossMargin()
{
  if ( this->revenue > 0 )
    return (1 - this->cogs / this->revenue);
  return 0;
}


EntryPoint.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "functions.h"

int    main   ()
{
    finance_state MyState( 500.0 , 300.0, 200.0, 100.0 );
    std::cout<< "my current ration is :\t"<< MyState.CurrentRatio()<<std::endl;
    std::cout<< "my Gross Margin is :\t"<< MyState.GetGrossMargin()<<std::endl;
    return 0;
}
It's working. Thank you so much. Really appreciate your help. Thanks again... :)
Hello Abhishekm,

A small doubt, if I want to use IF...ELSE statement. How to use inside the program?
Last edited on
I would recommend you go through some basic tutorials for the C++ language. The one in this website is a nice tutorial for beginners.

The syntax for 'if else' statement is as follows:
1
2
3
4
5
if ( condition == true ) {
  //do something
} else {
  // do something else
}
OK... Thank you :)
Topic archived. No new replies allowed.