Home » C++ programs

C++ | Create an object of a class and access class attributes

Here, we are going to learn how to create an object of a class and access class attributes in C++ programming language?
Submitted by IncludeHelp, on February 20, 2020

In the below program, we are creating a C++ program to create an object of a class and access class attributes.

/* C++ program to create an object of a class 
and access class attributes */

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

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

int main()
{
    // object creation
    Student std;

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

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

    return 0;
}

Output

Student's Roll No.: 101
Student's Name: Shivang Yadav
Student's Percentage: 98.2





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.