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

Assignment.docx

The document contains multiple C++ code examples demonstrating various concepts such as classes, constructors, destructors, and friend functions. Each example illustrates different functionalities, including temperature conversion, student information management, and distance addition. The code snippets are structured with main functions to execute the defined classes and their methods.

Uploaded by

harsh.24bce10334
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)
7 views

Assignment.docx

The document contains multiple C++ code examples demonstrating various concepts such as classes, constructors, destructors, and friend functions. Each example illustrates different functionalities, including temperature conversion, student information management, and distance addition. The code snippets are structured with main functions to execute the defined classes and their methods.

Uploaded by

harsh.24bce10334
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/ 7

Ans1: #include <iostream>

class Temperature {
private:
double celsius;
double fahrenheit;

double toFahrenheit(double c) {
return (c * 9.0 / 5.0) + 32;
}

double toCelsius(double f) {
return (f - 32) * 5.0 / 9.0;
}

public:
Temperature(double temp, bool isCelsius = true) {
if (isCelsius) {
celsius = temp;
fahrenheit = toFahrenheit(temp);
} else {
fahrenheit = temp;
celsius = toCelsius(temp);
}
}
void setCelsius(double temp) {
celsius = temp;
fahrenheit = toFahrenheit(temp);
}

void setFahrenheit(double temp) {


fahrenheit = temp;
celsius = toCelsius(temp);
}

double getCelsius() const {


return celsius;
}

double getFahrenheit() const {


return fahrenheit;
}
};

int main() {
Temperature temp1(25); // 25°C
std::cout << "Temperature: " << temp1.getCelsius() << "°C = " << temp1.getFahrenheit()
<< "°F" << std::endl;

temp1.setFahrenheit(98.6); // Setting temperature in Fahrenheit


std::cout << "Temperature: " << temp1.getCelsius() << "°C = " << temp1.getFahrenheit()
<< "°F" << std::endl;
return 0;
}

Ans2: #include <iostream>


#include <cstring>

class Student {
private:
char name[50];
int marks1;
int marks2;

public:
void input() {
std::cout << "Enter student name: ";
std::cin.getline(name, 50);
std::cout << "Enter marks1: ";
std::cin >> marks1;
std::cout << "Enter marks2: ";
std::cin >> marks2;
}

void disp() const {


std::cout << "Student Name: " << name << std::endl;
std::cout << "Total Marks: " << (marks1 + marks2) / 2.0 << std::endl;
}
};

int main() {
Student s;
std::cin.ignore();
s.input();
s.disp();
return 0;
}

Ans3: A class in C++ is a blueprint or template for creating objects. It defines data
members (variables) and member functions (methods) that operate on the data. A class
encapsulates data and behavior, supporting object-oriented programming (OOP).
An object is an instance of a class. When you create an object, it gets its own copy of the
data members and can use the class's methods.
You can access the members of a class using the dot (.) operator for an object.

Ans4: #include <iostream>


#include <string>

class Student {
private:
std::string name;
int rollNumber;
double marks;

public:
Student(std::string studentName, int studentRollNumber, double studentMarks) {
name = studentName;
rollNumber = studentRollNumber;
marks = studentMarks;
}

~Student() {
std::cout << "Student object destroyed for " << name << std::endl;
}

void displayDetails() const {


std::cout << "Name: " << name << std::endl;
std::cout << "Roll Number: " << rollNumber << std::endl;
std::cout << "Marks: " << marks << std::endl;
}
};

int main() {
Student s("John Doe", 101, 85.5);
s.displayDetails();
return 0;
}

Ans5: #include <iostream>


using namespace std;

class Distance {
private:
int meters;

public:
Distance(int m = 0) : meters(m) {}

friend Distance addDistance(const Distance &d1, const Distance &d2);

void display() const {


cout << "Distance: " << meters << " meters" << endl;
}
};

Distance addDistance(const Distance &d1, const Distance &d2) {


return Distance(d1.meters + d2.meters);
}

int main() {
Distance d1(50), d2(30);
Distance d3 = addDistance(d1, d2);

cout << "First ";


d1.display();
cout << "Second ";
d2.display();
cout << "Sum ";
d3.display();
return 0;
}

You might also like