Accessing member variables and functions of objects created on the heap. - C++ Operator

C++ examples for Operator:new

Description

Accessing member variables and functions of objects created on the heap.

Demo Code

#include <iostream> 
 
class SimpleCat //from   w ww.j a  va2s.  c o m
{ 
public: 
    SimpleCat() { itsAge = 2; } 
    ~SimpleCat() {} 
    int GetAge() const { return itsAge; } 
    void SetAge(int age) { itsAge = age; } 
private: 
    int itsAge; 
}; 
 
int main() 
{ 
    SimpleCat *Frisky = new SimpleCat; 
    std::cout << "Frisky is " << Frisky->GetAge() << " years old" << std::endl; 
 
    Frisky->SetAge(5); 
    std::cout << "Frisky is " << Frisky->GetAge() << " years old" << std::endl; 
 
    delete Frisky; 
    return 0; 
}

Result


Related Tutorials