Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9121705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:55:21+00:00 2026-06-17T05:55:21+00:00

When we declare variables in a class and then when we assign the value

  • 0

When we declare variables in a class and then when we assign the value of those variables, for example like this

class c {

    public :

    int x;
    int x2;

    c () {
        x = 0;
        x2 = 0;

        scanf ("%d", &x); and now we're gonna input for example 10
    } 
};

each time the class is used, I mean each time the constructor is called, the value of x becomes 0 again since it is initialized as zero in the constructor. However if we don’t initialize the value, there will be errors.

My question is that how can we keep the value of the variable when we call the constructor again and again so that it doesn’t become zero ?

Edit:

void example () {

int i;
scanf ("%d", &i); 
switch (i) {
case 1 : {Object ob1; system ("cls"); menu ();} // this object contains a value like 20 
case 2 : {Object ob2; system ("cls"); menu ();} 
}  

}

There is another switch case in Object 1 which includes an option to go back to a main menu, now if I enter 1 again go back to object 1 I cannot see the value 20, it will be 0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:55:22+00:00Added an answer on June 17, 2026 at 5:55 am

    Do you understand the difference between classes and objects/instances? Classes are merely a “Cookie-cutter” for objects. You don’t “call” a constructor as such, but you create an instance of your class (which implicitely calls the constructor):

    c myObj; 
    c anotherObj; 
    

    This code will create two instances of class c, both with their own version of x1 and x2. It’s true that the constructor is run a second time when creating anotherObj, but it operates on totally different memory. So the values of x1 and x2 in myObj won’t be touched.

    Edit: The point of class member functions is that they operate on an implicit additional parameter named this. You could imagine that the “constructor call” actually looks like that (Just for illustrative purposes, not meant to be valid code):

    c* this = malloc(sizeof(c));
    constructor(c);
    // With "constructor" actually being:
    
    void ctor(c* this) {
        this->x1 = 0;
        this->x2 = 0;
        // ..
    }
    

    That can also be achieved in C – but in C++, it happens implicitly, without you having to write code like this. You just write c newObj; or c* obj = new c;.

    Apart from that: Member variables should be privat, and your mixing C library code (scanf) with C++ classes – use iostreams for input/output in C++.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can declare private static member variables in a class, but what does it
I'm trying to declare a global variable from within a class like so: class
I have a variable declared like this in a class: Entity *array[BOARD_SIZE][BOARD_SIZE]; I need
I declare a variable and some methods in the global nsobject class like @interface
I have a abstract class where I want to declare final variables. However, I
I am trying to declare variables in class file of a control and to
If PHP is stateless then even if you declare variables as static they do
I've noticed that some generated classes only declare class properties/variables via @property, and don't
There are situations where I declare member variables at the top of my class
I have a game which have 5 class/level.Each class contain almost same variables.Now i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.