C++ Class Objects are several types. They are array of objects, object classes, global objects and local class objects,. In this section of C++ example programs, we are going to discuss about 3 source codes. These three C++ tutorial codes are specially designed for array of objects. Beginners like students and professionals can use this sample CPP source code for free of cost.
Here you can find the 3 different programs for C++ class objects. These three tutorials are very simple to understand. There will be the simple constructors and destructors for assigning values. Easy to follow like classes and simple methods are available in the following example codes.
Array of objects in C++ program:
#include<iostream.h> #include<conio.h> class c1 // Class definition { int m; int n; public: //Access specifiers c1(int o, int p) // Constructor with 2 parameters { m=o; n=p; } int getki() { return n; } int getih() { return m; } }; int main() // Main function { clrscr(); c1 objj[3]={c1(2,1),c1(2,4),c1(3,5)}; int i; for(i=0;i<3;i++) { cout<<objj[i].getih(); // Output display cout<< ","; cout<<objj[i].getki()<<"\n"; } getch(); return 0; }
Example C++ Program 2:
- Read: Learn English Quickly
- Worth Read: C++ Interview Questions
#include <iostream.h> #include <conio.h> class c1 { int x; public: c1(int y) //Simple constructor in C++ { x=y; } int getinp() // Method for getting an input { return x; } }; int main() { clrscr(); c1 objj[3] = {1, 4, 3}; // Object creation & Values initializing int x; for(x=0;x<3;x++) cout<<objj[x].getinp()<< "\t\n"; getch(); return 0; }
C++ Sample source code 3:
[SEE: C Program Tutorials] & [IT Companies Details]
#include<iostream.h> #include<conio.h> class c1 { int m; int n; public: c1(int o, int p) // Simple constructor with 2 arguments { m=o; n=p; } int inputn() {return n;} //Method definition int inputm() {return m;} // Function definition }; int main() // Main method { clrscr(); cl objj[3] = { cl(2, 2),cl(2, 3),cl(4, 5)}; int n; for(n=0;n<3;n++) { cout<<objj[n].inputm(); // Output display cout<< ","; cout<<objj[n].inputn() << "\n"; } getch(); return 0; }