Important C++ Programs Based on Classes and Objects with Output
Classes and Objects are fundamental concepts of Object-Oriented Programming (OOP) in C++. A class combines data members and member functions into a single unit, while an object is an instance of a class. In this article, we will learn 12 important C++ practical programs based on classes and objects, including student records, employee records, static members, friend functions, friend classes, arrays of objects and the this pointer.
Classes और Objects, C++ में Object-Oriented Programming (OOP) के fundamental concepts हैं। Class data members और member functions को एक unit में combine करती है, जबकि object किसी class का instance होता है। इस article में हम Classes और Objects पर आधारित 12 महत्वपूर्ण C++ programs सीखेंगे।
1. C++ Program to Demonstrate a Class and Its Objects
This program demonstrates how to define a class, create its objects and access data members and member functions.
यह program दिखाता है कि class कैसे define की जाती है, उसके objects कैसे बनाए जाते हैं और data members तथा member functions को कैसे access किया जाता है।
Program:
#include <iostream>
using namespace std;
class Student {
public:
int rollNo;
string name;
void display() {
cout << "Roll Number: " << rollNo << endl;
cout << "Name: " << name << endl;
}
};
int main() {
Student s1, s2;
s1.rollNo = 101;
s1.name = "Amit";
s2.rollNo = 102;
s2.name = "Rahul";
cout << "First Student:" << endl;
s1.display();
cout << "\nSecond Student:" << endl;
s2.display();
return 0;
}Output:
First Student:
Roll Number: 101
Name: Amit
Second Student:
Roll Number: 102
Name: RahulExplanation: The program defines a class named Student with two data members and one member function. Two objects, s1 and s2, are created and used to store and display different student information.
व्याख्या: इस program में Student नाम की class बनाई गई है। इसके दो objects s1 और s2 अलग-अलग students की information store और display करते हैं।
2. C++ Program to Create a Class for Storing Student Information
This program creates a Student class to store and display student information such as roll number, name and marks.
यह program student की roll number, name और marks जैसी information store तथा display करने के लिए Student class बनाता है।
Program:
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int rollNo;
string name;
float marks;
public:
void getData() {
cout << "Enter Roll Number: ";
cin >> rollNo;
cout << "Enter Name: ";
cin >> name;
cout << "Enter Marks: ";
cin >> marks;
}
void displayData() {
cout << "\nStudent Information" << endl;
cout << "Roll Number: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Marks: " << marks << endl;
}
};
int main() {
Student s1;
s1.getData();
s1.displayData();
return 0;
}Sample Output:
Enter Roll Number: 101
Enter Name: Amit
Enter Marks: 85.5
Student Information
Roll Number: 101
Name: Amit
Marks: 85.5Explanation: The Student class contains three private data members. The getData() function accepts information, while the displayData() function displays the stored information.
व्याख्या: Student class में तीन private data members हैं। getData() function student की information input करता है और displayData() function उसे display करता है।
3. C++ Program to Load and Display Records of N Students Using a Class
This program uses an array of objects to input and display records of N students.
इस program में array of objects का उपयोग करके N students के records input और display किए जाते हैं।
Program:
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int rollNo;
string name;
float marks;
public:
void input() {
cout << "Enter Roll Number: ";
cin >> rollNo;
cout << "Enter Name: ";
cin >> name;
cout << "Enter Marks: ";
cin >> marks;
}
void display() {
cout << rollNo << "\t"
<< name << "\t"
<< marks << endl;
}
};
int main() {
int n;
cout << "Enter Number of Students: ";
cin >> n;
if (n <= 0) {
cout << "Invalid number of students.";
return 0;
}
Student s[100];
if (n > 100) {
cout << "Maximum 100 students allowed.";
return 0;
}
for (int i = 0; i < n; i++) {
cout << "\nEnter Details of Student "
<< i + 1 << endl;
s[i].input();
}
cout << "\nStudent Records\n";
cout << "Roll No\tName\tMarks\n";
for (int i = 0; i < n; i++) {
s[i].display();
}
return 0;
}Sample Output:
Enter Number of Students: 3
Enter Details of Student 1
Enter Roll Number: 101
Enter Name: Amit
Enter Marks: 85
Enter Details of Student 2
Enter Roll Number: 102
Enter Name: Rahul
Enter Marks: 90
Enter Details of Student 3
Enter Roll Number: 103
Enter Name: Priya
Enter Marks: 92
Student Records
Roll No Name Marks
101 Amit 85
102 Rahul 90
103 Priya 92Explanation: An array s[100] stores objects of the Student class. A loop accepts the records, and another loop displays all the records.
व्याख्या: s[100] array में Student class के objects store होते हैं। एक loop सभी students के records input करता है और दूसरा loop सभी records display करता है।
Note: This program supports up to 100 students. The input check ensures that the array is not accessed beyond its capacity.
4. C++ Program to Load and Display Records of N Employees Using a Class
This program creates an Employee class to store employee ID, name and salary. It accepts and displays records of N employees.
यह program employee की ID, name और salary store करने के लिए Employee class बनाता है। इसमें N employees के records input और display किए जाते हैं।
Program:
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
int empId;
string name;
double salary;
public:
void input() {
cout << "Enter Employee ID: ";
cin >> empId;
cout << "Enter Employee Name: ";
cin >> name;
cout << "Enter Salary: ";
cin >> salary;
}
void display() {
cout << empId << "\t"
<< name << "\t"
<< salary << endl;
}
};
int main() {
int n;
cout << "Enter Number of Employees: ";
cin >> n;
if (n <= 0 || n > 100) {
cout << "Enter a number between 1 and 100.";
return 0;
}
Employee e[100];
for (int i = 0; i < n; i++) {
cout << "\nEnter Employee "
<< i + 1 << " Details\n";
e[i].input();
}
cout << "\nEmployee Records\n";
cout << "ID\tName\tSalary\n";
for (int i = 0; i < n; i++) {
e[i].display();
}
return 0;
}Sample Output:
Enter Number of Employees: 2
Enter Employee 1 Details
Enter Employee ID: 201
Enter Employee Name: Amit
Enter Salary: 25000
Enter Employee 2 Details
Enter Employee ID: 202
Enter Employee Name: Rahul
Enter Salary: 30000
Employee Records
ID Name Salary
201 Amit 25000
202 Rahul 30000Explanation: The Employee class contains private data members for employee ID, name and salary. The array e[100] stores multiple employee objects.
व्याख्या: Employee class में employee ID, name और salary के private data members हैं। e[100] array का उपयोग करके multiple employee objects के records store किए जाते हैं।
5. C++ Program to Load and Display Records of N Products Using a Class
This program creates a Product class to store product ID, product name and price.
यह program product ID, product name और price store करने के लिए Product class बनाता है।
Program:
#include <iostream>
#include <string>
using namespace std;
class Product {
private:
int productId;
string productName;
double price;
public:
void input() {
cout << "Enter Product ID: ";
cin >> productId;
cout << "Enter Product Name: ";
cin >> productName;
cout << "Enter Product Price: ";
cin >> price;
}
void display() {
cout << productId << "\t"
<< productName << "\t"
<< price << endl;
}
};
int main() {
int n;
cout << "Enter Number of Products: ";
cin >> n;
if (n <= 0 || n > 100) {
cout << "Enter a number between 1 and 100.";
return 0;
}
Product p[100];
for (int i = 0; i < n; i++) {
cout << "\nEnter Product "
<< i + 1 << " Details\n";
p[i].input();
}
cout << "\nProduct Records\n";
cout << "ID\tName\tPrice\n";
for (int i = 0; i < n; i++) {
p[i].display();
}
return 0;
}Sample Output:
Enter Number of Products: 3
Enter Product 1 Details
Enter Product ID: 1
Enter Product Name: Laptop
Enter Product Price: 45000
Enter Product 2 Details
Enter Product ID: 2
Enter Product Name: Mouse
Enter Product Price: 500
Enter Product 3 Details
Enter Product ID: 3
Enter Product Name: Keyboard
Enter Product Price: 1200
Product Records
ID Name Price
1 Laptop 45000
2 Mouse 500
3 Keyboard 1200Explanation: The Product class stores product details using private data members. The program uses an array of objects to manage multiple product records.
व्याख्या: Product class private data members के माध्यम से product की details store करती है। Multiple product records manage करने के लिए array of objects का उपयोग किया गया है।
6. C++ Program to Demonstrate Member Functions
Member functions are functions declared inside a class. They are used to perform operations on the data members of that class.
Member functions वे functions होते हैं जिन्हें class के अंदर declare किया जाता है। इनका उपयोग class के data members पर operations perform करने के लिए किया जाता है।
Program:
#include <iostream>
using namespace std;
class Calculator {
private:
int a, b;
public:
void input() {
cout << "Enter Two Numbers: ";
cin >> a >> b;
}
int add() {
return a + b;
}
int subtract() {
return a - b;
}
int multiply() {
return a * b;
}
void display() {
cout << "Addition = " << add() << endl;
cout << "Subtraction = " << subtract() << endl;
cout << "Multiplication = " << multiply() << endl;
}
};
int main() {
Calculator c;
c.input();
c.display();
return 0;
}Sample Output:
Enter Two Numbers: 20 10
Addition = 30
Subtraction = 10
Multiplication = 200Explanation: The class Calculator contains member functions input(), add(), subtract(), multiply() and display(). These functions perform different operations on the class data members.
व्याख्या: Calculator class में अलग-अलग member functions बनाए गए हैं। ये functions input लेने, addition, subtraction और multiplication करने तथा result display करने का कार्य करते हैं।
7. C++ Program to Demonstrate Access Specifiers
Access specifiers define the accessibility of class members. C++ provides three main access specifiers: public, private and protected.
Access specifiers यह निर्धारित करते हैं कि class के members को कहाँ से access किया जा सकता है। C++ में तीन मुख्य access specifiers हैं: public, private और protected।
Program:
#include <iostream>
using namespace std;
class Base {
public:
int publicData;
private:
int privateData;
protected:
int protectedData;
public:
void setData() {
publicData = 10;
privateData = 20;
protectedData = 30;
}
void displayPrivate() {
cout << "Private Data = "
<< privateData << endl;
}
};
class Derived : public Base {
public:
void displayProtected() {
cout << "Protected Data = "
<< protectedData << endl;
}
};
int main() {
Derived obj;
obj.setData();
cout << "Public Data = "
<< obj.publicData << endl;
obj.displayPrivate();
obj.displayProtected();
return 0;
}Output:
Public Data = 10
Private Data = 20
Protected Data = 30Explanation:
publicDatacan be accessed from outside the class.privateDatacan be accessed only by member functions and friends of the class.protectedDatacan be accessed by member functions and friends, and by suitable derived-class member functions.
व्याख्या:
publicDataको class के बाहर से access किया जा सकता है।privateDataको केवल class के member functions और friends access कर सकते हैं।protectedDataको class के member functions, friends तथा derived class के उपयुक्त member functions access कर सकते हैं।
8. C++ Program to Demonstrate Static Data Members and Static Member Functions
A static data member is shared by all objects of a class. A static member function can access static members directly without requiring an object.
Static data member class के सभी objects के बीच shared होता है। Static member function को object बनाए बिना call किया जा सकता है और यह static members को directly access कर सकता है।
Program:
#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
static int count;
public:
Student(int r) {
rollNo = r;
count++;
}
void display() {
cout << "Roll Number = "
<< rollNo << endl;
}
static void showCount() {
cout << "Total Students = "
<< count << endl;
}
};
int Student::count = 0;
int main() {
Student s1(101);
Student s2(102);
Student s3(103);
s1.display();
s2.display();
s3.display();
Student::showCount();
return 0;
}Output:
Roll Number = 101
Roll Number = 102
Roll Number = 103
Total Students = 3Explanation:
The static data member
countis shared by all objects.Each time a
Studentobject is created, the constructor incrementscount.The static member function
showCount()displays the total number of objects created.The static data member is defined outside the class using the scope resolution operator
::.
व्याख्या:
Static data member
countसभी objects के बीच shared होता है।जब भी कोई
Studentobject बनता है, constructorcountको increment करता है।Static member function
showCount()total objects की संख्या display करता है।Static data member को class के बाहर scope resolution operator
::की सहायता से define किया गया है।
9. C++ Program to Demonstrate Friend Function
A friend function is a non-member function that is allowed to access the private and protected members of a class.
Friend function एक non-member function होता है, जिसे किसी class के private और protected members को access करने की अनुमति दी जाती है।
Program:
#include <iostream>
using namespace std;
class Number {
private:
int value;
public:
Number(int v) {
value = v;
}
friend void display(Number n);
};
void display(Number n) {
cout << "Value = " << n.value << endl;
}
int main() {
Number obj(100);
display(obj);
return 0;
}Output:
Value = 100Explanation:
The function
display()is declared as a friend inside theNumberclass.It is defined outside the class.
Although it is not a member function, it can access the private data member
value.
व्याख्या:
display()function कोNumberclass में friend घोषित किया गया है।इसे class के बाहर define किया गया है।
यह member function नहीं है, फिर भी private data member
valueको access कर सकता है।
10. C++ Program to Demonstrate Friend Class
A friend class is a class whose member functions are allowed to access the private and protected members of another class.
Friend class ऐसी class होती है जिसके member functions को दूसरी class के private और protected members access करने की अनुमति होती है।
Program:
#include <iostream>
using namespace std;
class A {
private:
int number;
public:
A() {
number = 100;
}
friend class B;
};
class B {
public:
void display(A obj) {
cout << "Number = "
<< obj.number << endl;
}
};
int main() {
A objA;
B objB;
objB.display(objA);
return 0;
}Output:
Number = 100Explanation:
Class B is declared as a friend of class A. Therefore, its member function display() can access the private data member number of class A.
व्याख्या:
Class B को class A की friend घोषित किया गया है। इसलिए class B का member function display(), class A के private data member number को access कर सकता है।
11. C++ Program to Demonstrate an Array of Objects
An array of objects is a collection of objects of the same class stored under a single array name. It is useful for managing multiple records.
Array of Objects एक ही class के multiple objects का collection होता है, जिन्हें एक array name के अंतर्गत store किया जाता है। इसका उपयोग multiple records manage करने के लिए किया जाता है।
Program:
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
int rollNo;
string name;
void input() {
cout << "Enter Roll Number: ";
cin >> rollNo;
cout << "Enter Name: ";
cin >> name;
}
void display() {
cout << rollNo << "\t"
<< name << endl;
}
};
int main() {
Student s[3];
for (int i = 0; i < 3; i++) {
cout << "\nEnter Student "
<< i + 1 << " Details\n";
s[i].input();
}
cout << "\nStudent Records\n";
cout << "Roll No\tName\n";
for (int i = 0; i < 3; i++) {
s[i].display();
}
return 0;
}Sample Output:
Enter Student 1 Details
Enter Roll Number: 101
Enter Name: Amit
Enter Student 2 Details
Enter Roll Number: 102
Enter Name: Rahul
Enter Student 3 Details
Enter Roll Number: 103
Enter Name: Priya
Student Records
Roll No Name
101 Amit
102 Rahul
103 PriyaExplanation: The statement Student s[3]; creates an array containing three objects of the Student class. The loop accesses each object using its array index.
व्याख्या: Student s[3]; statement से Student class के तीन objects का array बनता है। Loop में array index की सहायता से प्रत्येक object को access किया जाता है।
12. C++ Program to Demonstrate the Use of this Pointer
The this pointer is a special pointer available inside non-static member functions. It points to the current object that invokes the member function.
this pointer एक special pointer है, जो non-static member functions के अंदर उपलब्ध होता है। यह उस current object को point करता है जिसने member function को call किया है।
Program:
#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
public:
void setData(int rollNo) {
this->rollNo = rollNo;
}
void display() {
cout << "Roll Number = "
<< this->rollNo << endl;
}
};
int main() {
Student s1, s2;
s1.setData(101);
s2.setData(102);
s1.display();
s2.display();
return 0;
}Output:
Roll Number = 101
Roll Number = 102Explanation:
The class has a private data member
rollNo.The
setData()function also has a parameter namedrollNo.this->rollNorefers to the data member of the current object.The parameter
rollNorefers to the value received by the function.
व्याख्या:
Class में
rollNoनाम का private data member है।setData()function में भीrollNoनाम का parameter है।this->rollNocurrent object के data member को refer करता है।केवल
rollNofunction के parameter को refer करता है।
Comments
Post a Comment