C++ source code for “global class objects” and “local class”: In this CPP tutorial, we are going to discuss about 3 various example programs. These three sample codes are coming under the category of “C++ Class objects“. It can be useful for several school, college students and professionals.
Those 3 sample codes are,
- Simple C++ program,
- C++ global class objects,
- C++ Local Class,
Simple C++ Program:
This code is very basic and it is for beginners understanding. Using this code we can display the string like “Hello - Welcome to Students3k”.
[SEE: C Program Tutorials] & [IT Companies Details]
// Header files #include<iostream.h> #include<conio.h> void main() // Main method { clrscr(); // Clears the previous O/P screen cout<<"Hello - Welcome to Students3k !!"; // Display getch(); }
C++ Global Class Objects:
Creating objects for global class. These objects are considered to be the global and it can be used in anywhere on this program. A class, access specifiers, constructor, destructor and local objects are using in this C++ tutorial. Code is given below.
- Read: Learn English Quickly
- Worth Read: C++ Interview Questions
#include<iostream.h> #include<conio.h> // Class definition class GlobObj { public: // Access specifiers int who; GlobObj(int id); // Constructor ~GlobObjs(); // Destructor }globob1(1), globob2(2); // Global objects GlobObj::GlobObj(int id) { cout<<"Initializing:"<<id<<"\n"; who=id; } GlobObj::~GlobObj() { cout<<"Destructing"<<who<<"\n"; } int main() // Main method { GlobObj localob1(3); GlobObj localob2(4); getch(); return 0; }
C++ Local Class:
A local class is declared and defined within a method definition. Local class can only use type names, static variables and enumerations. It can be accessed by the SCOPE variable. Example source code is given below.
#include<iostream.h> #include<conio.h> void xy(); // Method declaration int main() { xy(); return 0; } void xy() // Method definition { class local // Local class { int k; public: // Access specifiers void display(int N) { k=N; } int input() { return k; } } O1; // Local class over O1.display(8); cout<<O1.input(); } using namespace std;