Day 49 – Classes and Objects in C++
Class 11 Computer Science
What are Classes and Objects? (Classes और Objects क्या हैं?)
A class in C++ is a blueprint for creating objects. It defines the structure and behavior of objects by encapsulating data (attributes) and methods (functions).
C++ में क्लास एक ब्लूप्रिंट है जिसका उपयोग ऑब्जेक्ट्स बनाने के लिए किया जाता है। यह डेटा (एट्रिब्यूट्स) और विधियों (फ़ंक्शन्स) को समेटकर ऑब्जेक्ट्स की संरचना और व्यवहार को परिभाषित करता है।
An object is an instance of a class, created using the class definition. It has attributes (data members) and behaviors (member functions).
ऑब्जेक्ट एक क्लास का इंस्टेंस है, जिसे क्लास की परिभाषा का उपयोग करके बनाया जाता है। इसमें एट्रिब्यूट्स (डेटा मेंबर्स) और व्यवहार (सदस्य फ़ंक्शन्स) होते हैं।
Defining a Class in C++ (C++ में क्लास को परिभाषित करना)
A class is defined using the class keyword followed by the class name. It contains data members and member functions.
C++ में class कीवर्ड का उपयोग करके क्लास को परिभाषित किया जाता है। इसमें डेटा मेंबर्स और सदस्य फ़ंक्शन्स होते हैं।
Syntax:
class ClassName {
public:
// Data members
// Member functions
};
Creating Objects in C++ (C++ में ऑब्जेक्ट्स बनाना)
Objects are created using the class name.
ऑब्जेक्ट्स को क्लास के नाम का उपयोग करके बनाया जाता है।
Syntax:
ClassName objectName;
Example:
class Car {
public:
string brand;
int year;
void displayDetails() {
cout << "Brand: " << brand << endl;
cout << "Year: " << year << endl;
}
};
int main() {
Car car1; // Object of class Car
car1.brand = "Honda";
car1.year = 2022;
car1.displayDetails();
return 0;
}
Output:
Brand: Honda Year: 2022
Access Specifiers in C++ (C++ में एक्सेस स्पेसिफायर्स)
Access specifiers define the scope of class members. There are three types:
एक्सेस स्पेसिफायर्स क्लास सदस्यों के स्कोप को परिभाषित करते हैं। तीन प्रकार होते हैं:
- Public: Members are accessible from outside the class.
सार्वजनिक: सदस्य क्लास के बाहर से एक्सेस किए जा सकते हैं। - Private: Members are accessible only within the class.
निजी: सदस्य केवल क्लास के भीतर एक्सेस किए जा सकते हैं। - Protected: Members are accessible within the class and derived classes.
सुरक्षित: सदस्य क्लास और डेरिव्ड क्लास के भीतर एक्सेस किए जा सकते हैं।
Examples of Classes and Objects in C++ (C++ में Classes और Objects के उदाहरण)
Example 1: Class with Public Members
#include <iostream>
using namespace std;
class Student {
public:
string name;
int rollNo;
void display() {
cout << "Name: " << name << endl;
cout << "Roll No: " << rollNo << endl;
}
};
int main() {
Student s1;
s1.name = "John";
s1.rollNo = 101;
s1.display();
return 0;
}
Output:
Name: John Roll No: 101
Example 2: Class with Private Members
#include <iostream>
using namespace std;
class Account {
private:
double balance;
public:
void setBalance(double amount) {
balance = amount;
}
double getBalance() {
return balance;
}
};
int main() {
Account acc;
acc.setBalance(5000);
cout << "Account Balance: " << acc.getBalance();
return 0;
}
Output:
Account Balance: 5000
Practice Questions
Multiple Choice Questions (MCQs)
- What is a class in C++?
(a) An object | (b) A blueprint for objects | (c) A function | (d) None - Which keyword is used to define a class?
(a) object | (b) class | (c) public | (d) None - What is an object in C++?
(a) An instance of a class | (b) A data type | (c) A function | (d) None - Which access specifier allows members to be accessed only within the class?
(a) Public | (b) Private | (c) Protected | (d) None - Which access specifier is used by default in a class?
(a) Public | (b) Private | (c) Protected | (d) None - What is the purpose of a constructor in a class?
(a) To initialize objects | (b) To create classes | (c) To define functions | (d) None - Which symbol is used to access members of a class through an object?
(a) . (dot) | (b) -> (arrow) | (c) * (pointer) | (d) None - What is a destructor in a class?
(a) A function to delete the object | (b) A function to initialize the object | (c) Both | (d) None - What is the output of the following code?
class A { public: void display() { cout << "Hello, World!"; } }; int main() { A obj; obj.display(); return 0; }(a) Hello, World! | (b) Error | (c) Nothing | (d) None - What does encapsulation achieve in a class?
(a) Code reusability | (b) Data hiding | (c) Polymorphism | (d) None
Answers to MCQs:
1: (b), 2: (b), 3: (a), 4: (b), 5: (b), 6: (a), 7: (a), 8: (a), 9: (a), 10: (b)
Short Answer Questions
- What are classes and objects in C++?
Answer: A class is a blueprint for objects, and an object is an instance of a class. - Explain the use of access specifiers in C++.
Answer: Access specifiers define the scope of class members (public, private, protected). - Write the syntax for defining a class in C++.
Answer:class ClassName { public: // Members }; - What is the purpose of encapsulation in C++?
Answer: Encapsulation secures data by binding it with methods in a single class. - Write a simple program to create a class and an object in C++.
Answer:class Demo { public: int num; void display() { cout << "Number: " << num; } }; int main() { Demo d; d.num = 5; d.display(); return 0; }
Long Answer Questions
- Explain the concept of classes and objects with examples.
- Write a program to demonstrate access specifiers in C++.
- Discuss the role of encapsulation in object-oriented programming.
Post a Comment