OOP
OOP
=} #include <iostream>
int main() {
int a, b, temp;
cin >> a;
cin >> b;
cout << "\nBefore swapping: a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "After swapping: a = " << a << ", b = " << b << endl; return 0;
–------–--------------*-*-*-*-*-*-*-*-*-*-*
=} #include <iostream>
Int main() {
Cout << “The square of “ << number << “ is: “ << square << endl;
Return 0;
-*-*-*-*-*-*-*-*-*-*-*-*-*---------
1) develop a problem to find the factorial of a given number using a for loop
=} #include <iostream>
int main() {
int number;
unsigned long long factorial = 1; // Use unsigned long long for large factorials
if (number < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else if (number == 0) {
} else {
for (int i = 1; i <= number; +i) { factorial *= i;
cout << "The factorial of " << number << " is " << factorial << "." << endl;
return 0;
-------*--*--*-*-*--*-*-*-*--------------
1) Development a program to declare a class student the data members are roll no. Name,
and marks. Accept and display data for one object of class student
=} #include <iostream>
#include <string>
class Student {
private:
int roll_no;
string name;
float marks;
public:
void acceptData() {
getline(cin, name);
void displayData() {
};
int main() {
Student student;
student.acceptData();
student.displayData();
return 0;
----*------*------*-------*------“-------*--
2) Write a c++ program to count number of object created with the help of static data
member
=} #include <iostream>
#include <string>
class Student {
private:
int roll_no;
string name;
float marks;
public:
void acceptData() {
getline(cin, name);
void displayData() {
};
int main() {
Student student;
student.acceptData();
student.displayData();
return 0;
--------*--------*---------*--------*-------
3) write a c++ program to declare a class student with data member as rollno, and name.
Declare a constructor to initialize data member of class display the data
=} #include <iostream>
#include <string>
class Student {
private:
int roll_no;
string name;
public:
Student(int r, string n) {
roll_no = r;
name = n;
void displayData() {
};
int main() {
// Create a Student object and initialize data members using the constructor
int roll;
string studentName;
getline(cin, studentName);
student.displayData();
return 0;
---*------*------*-----*------*-----*-----*
4) Write a c++ program to declare two classes with data member as m1 and m2
respectively use friend function to calculate average of two (m1, m2) marks and display it in
c++
=} #include <iostream>
// Class1 declaration
class Class1 {
private:
public:
// Constructor to initialize m1
Class1(float marks) {
m1 = marks;
};
// Class2 declaration
class Class2 {
private:
public:
// Constructor to initialize m2
Class2(float marks) {
m2 = marks;
};
cout << "The average of the two marks (" << obj1.m1 << " and " << obj2.m2 << ") is: " <<
average << endl;
int main() {
Class1 obj1(marks1);
Class2 obj2(marks2);
calculateAverage(obj1, obj2);
return 0;