Object Creation Example in C++


This program defines a class called MyClass with two member variables, num and str of type int and string respectively. In the main() function, an object obj of MyClass is created and its member variables num and str are initialized with values 15 and "Tutor Joes". The values are then printed to the console using cout.

Source Code

#include<iostream>
using namespace std;
class MyClass
{
   public:
      int num;
      string str;
};
int main()
{
  MyClass obj;
  obj.num = 15;
  obj.str = "Tutor Joes";
  cout<<obj.num<<"\n";
  cout<<obj.str;
  return 0;
}
To download raw file Click Here

Output

15
Tutor Joes

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples