C++ Classes and Objects
Learn C++ class and object concepts using Employee class with properties: name, age, and salary.
C++ में class और object सीखें Employee class के साथ, जिसमें name, age और salary properties होंगी।
Example 1: Basic Class and Object
#include
using namespace std;
class Employee {
public:
string name;
int age;
double salary;
};
int main() {
Employee e;
e.name = "John";
e.age = 30;
e.salary = 50000;
cout << "Name: " << e.name << endl;
cout << "Age: " << e.age << endl;
cout << "Salary: " << e.salary << endl;
return 0;
}
Example of Example 1: Basic Class and Object in C++.
C++ में Example 1: Basic Class and Object का उदाहरण।
Example 2: Constructor
#include
using namespace std;
class Employee {
public:
string name;
int age;
double salary;
Employee() {
name = "Default";
age = 25;
salary = 40000;
}
};
int main() {
Employee e;
cout << e.name << " " << e.age << " " << e.salary << endl;
return 0;
}
Example of Example 2: Constructor in C++.
C++ में Example 2: Constructor का उदाहरण।
Example 3: Parameterized Constructor
#include
using namespace std;
class Employee {
public:
string name;
int age;
double salary;
Employee(string n, int a, double s) {
name = n;
age = a;
salary = s;
}
};
int main() {
Employee e("Alice", 28, 60000);
cout << e.name << " " << e.age << " " << e.salary << endl;
return 0;
}
Example of Example 3: Parameterized Constructor in C++.
C++ में Example 3: Parameterized Constructor का उदाहरण।
Example 4: Default Constructor
#include
using namespace std;
class Employee {
public:
string name;
int age;
double salary;
Employee() {
cout << "Default Constructor Called" << endl;
}
};
int main() {
Employee e;
return 0;
}
Example of Example 4: Default Constructor in C++.
C++ में Example 4: Default Constructor का उदाहरण।
Example 5: Setter and Getter Methods
#include
using namespace std;
class Employee {
string name;
int age;
double salary;
public:
void setData(string n, int a, double s) {
name = n;
age = a;
salary = s;
}
void display() {
cout << name << " " << age << " " << salary << endl;
}
};
int main() {
Employee e;
e.setData("Bob", 35, 70000);
e.display();
return 0;
}
Example of Example 5: Setter and Getter Methods in C++.
C++ में Example 5: Setter and Getter Methods का उदाहरण।
Example 6: Array of Objects
#include
using namespace std;
class Employee {
public:
string name;
int age;
double salary;
void setData(string n, int a, double s) {
name = n;
age = a;
salary = s;
}
void display() {
cout << name << " " << age << " " << salary << endl;
}
};
int main() {
Employee arr[2];
arr[0].setData("Tom", 29, 55000);
arr[1].setData("Jerry", 31, 58000);
for(int i=0; i<2; i++) {
arr[i].display();
}
return 0;
}
Example of Example 6: Array of Objects in C++.
C++ में Example 6: Array of Objects का उदाहरण।
Example 7: Dynamic Allocation
#include
using namespace std;
class Employee {
public:
string name;
int age;
double salary;
void setData(string n, int a, double s) {
name = n;
age = a;
salary = s;
}
void display() {
cout << name << " " << age << " " << salary << endl;
}
};
int main() {
Employee* e = new Employee;
e->setData("Sam", 40, 90000);
e->display();
delete e;
return 0;
}
Example of Example 7: Dynamic Allocation in C++.
C++ में Example 7: Dynamic Allocation का उदाहरण।
Example 8: Copy Constructor
#include
using namespace std;
class Employee {
public:
string name;
int age;
double salary;
Employee(string n, int a, double s) {
name = n;
age = a;
salary = s;
}
Employee(const Employee &e) {
name = e.name;
age = e.age;
salary = e.salary;
}
void display() {
cout << name << " " << age << " " << salary << endl;
}
};
int main() {
Employee e1("Mike", 27, 65000);
Employee e2 = e1;
e2.display();
return 0;
}
Example of Example 8: Copy Constructor in C++.
C++ में Example 8: Copy Constructor का उदाहरण।
Example 9: Static Member
#include
using namespace std;
class Employee {
public:
static int count;
Employee() {
count++;
}
};
int Employee::count = 0;
int main() {
Employee e1, e2, e3;
cout << "Total Employees: " << Employee::count << endl;
return 0;
}
Example of Example 9: Static Member in C++.
C++ में Example 9: Static Member का उदाहरण।
Example 10: Inheritance
#include
using namespace std;
class Employee {
public:
string name;
int age;
double salary;
};
class Manager : public Employee {
public:
string department;
void setData(string n, int a, double s, string d) {
name = n;
age = a;
salary = s;
department = d;
}
void display() {
cout << name << " " << age << " " << salary << " " << department << endl;
}
};
int main() {
Manager m;
m.setData("Sara", 32, 80000, "HR");
m.display();
return 0;
}
Example of Example 10: Inheritance in C++.
C++ में Example 10: Inheritance का उदाहरण।