Home » C++ programs

C++ | Create multiple objects of a class

Here, we are going to learn how to create multiple objects of a class in C++ programming language?
Submitted by IncludeHelp, on February 20, 2020

In the below program, we are creating a C++ program to create multiple objects of a class.

/* C++ program to create multiple objects of a class */

#include <iostream>
#include <string>
using namespace std;

// class definition
// "student" is a class
class Student {
public: // Access specifier
    int rollNo;
    string stdName;
    float perc;
};

int main()
{
    // multiple object creation
    Student std1, std2;

    // Accessing attributes and setting the values
    std1.rollNo = 101;
    std1.stdName = "Shivang Yadav";
    std1.perc = 98.20f;

    std2.rollNo = 102;
    std2.stdName = "Hrithik Chandra Prasad";
    std2.perc = 99.99f;

    // Printing the values
    cout << "student 1..."
         << "\n";
    cout << "Student's Roll No.: " << std1.rollNo << "\n";
    cout << "Student's Name: " << std1.stdName << "\n";
    cout << "Student's Percentage: " << std1.perc << "\n";

    cout << "student 2..."
         << "\n";
    cout << "Student's Roll No.: " << std2.rollNo << "\n";
    cout << "Student's Name: " << std2.stdName << "\n";
    cout << "Student's Percentage: " << std2.perc << "\n";

    return 0;
}

Output

student 1...
Student's Roll No.: 101 
Student's Name: Shivang Yadav 
Student's Percentage: 98.2
student 2...
Student's Roll No.: 102 
Student's Name: Hrithik Chandra Prasad
Student's Percentage: 99.99





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.