Click here to Skip to main content
15,913,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<iostream>
#include<conio.h>


using namespace std;

class base
{

public:
    base()
    {
        int a =10;
        int b =20;
        int c = a+b;
    }


};

int main()
{

  base a;

  getch();

   return 0;

}
Posted
Updated 12-Mar-15 8:42am
v2
Comments
ZurdoDev 12-Mar-15 14:20pm    
Need more details.
Sergey Alexandrovich Kryukov 12-Mar-15 16:59pm    
I would advise you to read at least one book programming with sufficient attention, from the very beginning to the very end, not skipping topics.
I also recommend to accept Solution 1 formally.
—SA
Member 11520335 12-Mar-15 21:18pm    
I just need the value of c, but no member varibale in that class like public c.. We have class with constructor .. thats it

1 solution

You cannot use the value of the (variable) c: it is a local variable (its lifetime is scoped in Base constructor, that is, after constructor execution, c doesn't exist anymore). Try:

C#
class base
{
public:
  int c;

  base()
  {
    int a =10;
    int b = 20;
    c = a + b;
  }
};

int main()
{
  base a;
  cout << "c = " << a.c << endl;
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 12-Mar-15 16:57pm    
5ed.
—SA
CPallini 12-Mar-15 17:10pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900