0% found this document useful (0 votes)
4 views4 pages

C++ Objects and Classes Explained

Uploaded by

rajkumarkus2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

C++ Objects and Classes Explained

Uploaded by

rajkumarkus2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ Object

In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.

In other words, object is an entity that has state and behavior. Here, state means data and
behavior means functionality.

Object is a runtime entity, it is created at runtime.

Object is an instance of a class. All the members of the class can be accessed through
object.

1. Student s1; //creating an object of Student

C++ Class
In C++, class is a group of similar objects. It is a template from which objects are created.
It can have fields, methods, constructors etc.

1. class Student
2. {
3. public:
4. int id; //field or data member
5. float salary; //field or data member
6. String name;//field or data member
7. }

C++ Object and Class Example


Let's see an example of class that has two fields: id and name. It creates instance of the
class, initializes the object and prints the object value.

1. #include <iostream>
2. using namespace std;
3. class Student {
4. public:
5. int id;//data member (also instance variable)
6. string name;//data member(also instance variable)
7. };
8. int main() {
9. Student s1; //creating an object of Student
10. [Link] = 201;
11. [Link] = "Sonoo Jaiswal";
12. cout<<[Link]<<endl;
13. cout<<[Link]<<endl;
14. return 0;
15. }

Output:

201
Sonoo Jaiswal

C++ Class Example: Initialize and Display data through


method
Let's see another example of C++ class where we are initializing and displaying object
through method.

1. #include <iostream>
2. using namespace std;
3. class Student {
4. public:
5. int id;//data member (also instance variable)
6. string name;//data member(also instance variable)
7. void insert(int i, string n)
8. {
9. id = i;
10. name = n;
11. }
12. void display()
13. {
14. cout<<id<<" "<<name<<endl;
15. }
16. };
17. int main(void) {
18. Student s1; //creating an object of Student
19. Student s2; //creating an object of Student
20. [Link](201, "Sonoo");
21. [Link](202, "Nakul");
22. [Link]();
23. [Link]();
24. return 0;
25. }

Output:

201 Sonoo
202 Nakul

C++ Class Example: Store and Display Employee


Information
Let's see another example of C++ class where we are storing and displaying employee
information using method.

1. #include <iostream>
2. using namespace std;
3. class Employee {
4. public:
5. int id;//data member (also instance variable)
6. string name;//data member(also instance variable)
7. float salary;
8. void insert(int i, string n, float s)
9. {
10. id = i;
11. name = n;
12. salary = s;
13. }
14. void display()
15. {
16. cout<<id<<" "<<name<<" "<<salary<<endl;
17. }
18. };
19. int main(void) {
20. Employee e1; //creating an object of Employee
21. Employee e2; //creating an object of Employee
22. [Link](201, "Sonoo",990000);
23. [Link](202, "Nakul", 29000);
24. [Link]();
25. [Link]();
26. return 0;
27. }

Output:

201 Sonoo 990000


202 Nakul 29000

You might also like