0% found this document useful (0 votes)
8 views

OOP

The document contains multiple C++ programs that demonstrate various programming concepts, including swapping integers, calculating the square of a number, finding the factorial using a for loop, and creating a class for student data. Each program includes user input, calculations, and output display. Additionally, there are examples of using constructors, friend functions, and static data members in classes.

Uploaded by

rajshreeborse25
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)
8 views

OOP

The document contains multiple C++ programs that demonstrate various programming concepts, including swapping integers, calculating the square of a number, finding the factorial using a for loop, and creating a class for student data. Each program includes user input, calculations, and output display. Additionally, there are examples of using constructors, friend functions, and static data members in classes.

Uploaded by

rajshreeborse25
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
You are on page 1/ 9

1) Write a program to swap two integers in c++

=} #include <iostream>

using namespace std;

int main() {

int a, b, temp;

// Input two integers

cout << "Enter the first number (a): ";

cin >> a;

cout << "Enter the second number (b): ";

cin >> b;

// Display original values

cout << "\nBefore swapping: a = " << a << ", b = " << b << endl;

// Swap the values using a temporary variable

temp = a;

a = b;

b = temp;

// Display swapped values

cout << "After swapping: a = " << a << ", b = " << b << endl; return 0;

–------–--------------*-*-*-*-*-*-*-*-*-*-*

2) write a c/c++program to accept a no. And display it’s square

=} #include <iostream>

Using namespace std;

Int main() {

Double number, square;


// Input a number

Cout << “Enter a number: “;

Cin >> number;

// Calculate the square

Square = number * number;

// Display the square

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>

using namespace std;

int main() {

int number;

unsigned long long factorial = 1; // Use unsigned long long for large factorials

// Input: Get the number from the user

cout << "Enter a number: ";

cin >> number;

// Check if the number is negative, zero, or positive

if (number < 0) {

cout << "Factorial is not defined for negative numbers." << endl;

} else if (number == 0) {

cout << "The factorial of 0 is 1." << endl;

} 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>

using namespace std;

class Student {

private:

int roll_no;

string name;

float marks;

public:

void acceptData() {

cout << "Enter roll number: ";

cin >> roll_no;

cin.ignore(); // To clear the input buffer

cout << "Enter name: ";

getline(cin, name);

cout << "Enter marks: ";


cin >> marks;

void displayData() {

cout << "\n--- Student Details ---" << endl;

cout << "Roll Number: " << roll_no << endl;

cout << "Name: " << name << endl;

cout << "Marks: " << marks << endl;

};

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>

using namespace std;

class Student {

private:

int roll_no;

string name;
float marks;

public:

void acceptData() {

cout << "Enter roll number: ";

cin >> roll_no;

cin.ignore(); // To clear the input buffer

cout << "Enter name: ";

getline(cin, name);

cout << "Enter marks: ";

cin >> marks;

void displayData() {

cout << "\n--- Student Details ---" << endl;

cout << "Roll Number: " << roll_no << endl;

cout << "Name: " << name << endl;

cout << "Marks: " << marks << endl;

};

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>

using namespace std;

class Student {

private:

int roll_no;

string name;

public:

// Constructor to initialize data members

Student(int r, string n) {

roll_no = r;

name = n;

void displayData() {

cout << "\n--- Student Details ---" << endl;

cout << "Roll Number: " << roll_no << endl;

cout << "Name: " << name << endl;

};

int main() {

// Create a Student object and initialize data members using the constructor

int roll;

string studentName;

cout << "Enter roll number: ";


cin >> roll;

cin.ignore(); // Clear the input buffer

cout << "Enter name: ";

getline(cin, studentName);

Student student(roll, 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>

using namespace std;

class Class1; // Forward declaration of Class1

class Class2; // Forward declaration of Class2

// Class1 declaration

class Class1 {

private:

float m1; // Data member for marks in Class1

public:

// Constructor to initialize m1

Class1(float marks) {

m1 = marks;

// Friend function declaration


friend void calculateAverage(Class1 obj1, Class2 obj2);

};

// Class2 declaration

class Class2 {

private:

float m2; // Data member for marks in Class2

public:

// Constructor to initialize m2

Class2(float marks) {

m2 = marks;

// Friend function declaration

friend void calculateAverage(Class1 obj1, Class2 obj2);

};

// Friend function definition

void calculateAverage(Class1 obj1, Class2 obj2) {

float average = (obj1.m1 + obj2.m2) / 2;

cout << "The average of the two marks (" << obj1.m1 << " and " << obj2.m2 << ") is: " <<
average << endl;

int main() {

// Create objects of Class1 and Class2

float marks1, marks2;

cout << "Enter marks for m1: ";

cin >> marks1;

cout << "Enter marks for m2: ";


cin >> marks2;

Class1 obj1(marks1);

Class2 obj2(marks2);

// Calculate and display the average using the friend function

calculateAverage(obj1, obj2);

return 0;

You might also like