Q:

C++ Class and Objects | Find output programs | Set 5

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Class and Objects (set 5).

Program 1:

#include <iostream>
using namespace std;

class Sample {
    int X;
    int* PTR = &X;

public:
    void set(int x) const;
    void print();
};

void Sample::set(int x) const
{
    *PTR = x;
}

void Sample::print()
{
    cout << *PTR - EOF << " ";
}

int main()
{
    Sample OB;
    Sample* S = &OB;

    S->set(10);
    S->print();

    return 0;
}

Program 2:

#include <iostream>
using namespace std;

class Sample {
private:
    int* X;

public:
    void init()
    {
        X = new int();
        *X = 5;
    }
    void fun()
    {
        *X = *X * *(&(*X)) + 10;

        cout << *X;
    }
};

int main()
{
    Sample OB;

    OB.init();
    OB.fun();

    return 0;
}

Program 3:

#include <iostream>
using namespace std;

class Sample {
private:
    int* X;
    int* Y;

public:
    void init()
    {
        X = new int();
        Y = new int();

        X = 10;
        Y = 20;
    }
    void fun()
    {
        *X = *X + *Y;
        cout << *X;
    }
};

int main()
{
    Sample OB;

    OB.init();
    OB.fun();

    return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

11

Explanation:

In the above program, we created class Sample that contain member X and a constant pointer PTR that contains the address of X, here we cannot relocate the pointer but we can change the value of X using pointer PTR.

Here, we defined two member functions set() and print() outside the class.

Here, set() is a const member function, but we can assign value using pointer variable in set() function.

In the main() function, we created object of class and created pointer initialized with object OB, and print() member function uses cout statement.

cout<< *PTR-EOF <<" ";

The value of EOF is -1 then, 10- -1 = 11.

Thus, 11 will be printed on the console screen.

Answer Program 2:

Output:

35

Explanation:

In the above program, we created a class Sample that contains an integer pointer X and then we initialize pointer X in init() member function using the new operator. And assign with value 5.

Now, come to the member function fun() and evaluate the expression,

*X = *X* *(&(*X))+10;
*X = 5 * 5+10;
*X = 25+10;
*X = 35;

Then the final value of *X is 35 that will be printed on the console screen.

Answer Program 3:

Output:

main.cpp: In member function ‘void Sample::init()’:
main.cpp:15:13: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
         X = 10;
             ^~
main.cpp:16:13: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
         Y = 20;
             ^~

Explanation:

The above program will generate errors. Because in the init() member function, we assigned integer value to the pointer variable, if we want to assign value to pointer variable then we need to use asterisk (*) operator like,

*X = 10;
*Y = 20;

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Constructor and Destructor | Find output progr... >>
<< C++ Class and Objects | Find output programs | Set...