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

This section contains the C++ find output programs with their explanations on C++ Class and Objects (set 5).
Submitted by Nidhi, on June 10, 2020

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;
}

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.

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;
}

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.

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;
}

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;





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.