Uploaded by Sn Hax

CPP practical program notes

advertisement
CPP practical program notes
WARNING: Kripiya dyhaan de is file
mai palindrome ya factorial jese
purane program bilkul nahi hai, is me
sirf naye or taaze cpp ke object
oriented related reflected reference
code hai!!!!!!!!!!!
1| convert minute into second and hour into minute.
#include <iostream>
int main() {
int minutes, hours;
// Input minutes
std::cout << "Enter the number of minutes: ";
std::cin >> minutes;
// Convert minutes to seconds
int seconds = minutes * 60;
std::cout << minutes << " minutes is equal to " << seconds
<< " seconds." << std::endl;
// Input hours
std::cout << "Enter the number of hours: ";
std::cin >> hours;
// Convert hours to minutes
int minutesInHours = hours * 60;
std::cout << hours << " hours is equal to " << minutesInHours
<< " minutes." << std::endl;
return 0;
}
8| #include <iostream>
int main() {
int choice;
float amount, convertedAmount;
std::cout << "Choose conversion option:\n";
std::cout << "1. Paisa to Rupees\n";
std::cout << "2. Rupees to Paisa\n";
std::cout << "Enter your choice (1 or 2): ";
std::cin >> choice;
std::cout << "Enter the amount: ";
std::cin >> amount;
if (choice == 1) {
convertedAmount = amount / 100.0;
std::cout << amount << " paisa is equal to " <<
convertedAmount << " rupees." << std::endl;
} else if (choice == 2) {
convertedAmount = amount * 100;
std::cout << amount << " rupees is equal to " <<
convertedAmount << " paisa." << std::endl;
} else {
std::cout << "Invalid choice. Please enter 1 or 2." <<
std::endl;
}
return 0;
}
12| wap to print a table
#include <iostream>
int main() {
int rows, columns;
std::cout << "Enter the number of rows: ";
std::cin >> rows;
std::cout << "Enter the number of columns: ";
std::cin >> columns;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= columns; ++j) {
std::cout << i * j << "\t";
}
std::cout << std::endl;
}
return 0;
}
14| wap to show student details using class and objects
WARNING: kripiya dhyaan de is wale program mai constructor
use huwa hai, to program thoda sa complex hai.
#include <iostream>
#include <string>
class Student {
private:
std::string name;
int rollNumber;
int age;
std::string course;
public:
// Constructor
Student(std::string n, int roll, int a, std::string c)
: name(n), rollNumber(roll), age(a), course(c) {}
// Display student information
void displayInfo() {
std::cout << "Name: " << name << std::endl;
std::cout << "Roll Number: " << rollNumber << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Course: " << course << std::endl;
}
};
int main() {
std::string name, course;
int rollNumber, age;
std::cout << "Enter student information:" << std::endl;
std::cout << "Name: ";
std::getline(std::cin, name);
std::cout << "Roll Number: ";
std::cin >> rollNumber;
std::cout << "Age: ";
std::cin >> age;
std::cin.ignore(); // Ignore the newline character
std::cout << "Course: ";
std::getline(std::cin, course);
// Create a Student object
Student student(name, rollNumber, age, course);
std::cout << "\nStudent Information:\n";
student.displayInfo();
return 0;
}
16| wap to implement unary operator overloading.
#include <iostream>
class Counter {
private:
int count;
public:
Counter(int initialCount = 0) : count(initialCount) {}
void display() {
std::cout << "Count: " << count << std::endl;
}
Counter operator++() {
++count;
return *this;
}
Counter operator++(int) {
Counter temp = *this;
++count;
return temp;
}
};
int main() {
Counter c1(5);
++c1;
c1.display(); // Output: Count: 6
Counter c2 = c1++;
c1.display(); // Output: Count: 7
c2.display(); // Output: Count: 6
return 0;
}
17| wap to implement binary operator overloading.
#include <iostream>
class ComplexNumber {
private:
int real;
int imaginary;
public:
ComplexNumber(int r = 0, int i = 0) : real(r), imaginary(i) {}
ComplexNumber operator+(const ComplexNumber& other) {
return ComplexNumber(real + other.real, imaginary +
other.imaginary);
}
void display() {
std::cout << real << " + " << imaginary << "i" << std::endl;
}
};
int main() {
ComplexNumber c1(2, 3);
ComplexNumber c2(1, 2);
ComplexNumber sum = c1 + c2;
sum.display(); // Output: 3 + 5i
return 0;
}
NOTE: baat aisi hain ki ahmad sir ne ye dono program karai hai
in practical session and it was a little more easier than this one,
samaj rahe ho na thoda sa he easy hai sirf!!!!
18| demonstrate function overloading.
#include <iostream>
#include <string>
void printMessage(int) {
std::cout << "Integer input not supported." << std::endl;
}
void printMessage(double) {
std::cout << "Double input not supported." << std::endl;
}
void printMessage(const std::string& message) {
std::cout << "Message: " << message << std::endl;
}
int main() {
std::string message = "Hello, World!";
printMessage(10);
// Output: Integer input not supported.
printMessage(3.14); // Output: Double input not supported.
printMessage(message); // Output: Message: Hello, World!
return 0;
}
NOTE: upar using namespace std; dal dena bar bar std dal ne
ki zarurat nahi padegi. Muft ki salah hai… lena hai to lo, warna.
19| demonstrate constructor overloading.
#include <iostream>
#include <string>
class Person {
private:
std::string name;
int age;
public:
// Default constructor
Person() {
name = "Unknown";
age = 0;
}
// Constructor with name parameter
Person(const std::string& n) {
name = n;
age = 0;
}
// Constructor with name and age parameters
Person(const std::string& n, int a) {
name = n;
age = a;
}
void displayInfo() {
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
}
};
int main() {
Person p1;
Person p2("John");
parameter
Person p3("Alice", 25);
and age parameters
// Using default constructor
// Using constructor with name
// Using constructor with name
p1.displayInfo();
std::cout << std::endl;
p2.displayInfo();
20| single level inheritance.
#include <iostream>
#include <string>
// Base class: Person
class Person {
protected:
std::string name;
public:
Person(const std::string& n) : name(n) {}
void displayInfo() {
std::cout << "Name: " << name << std::endl;
}
};
// Derived class: Student
class Student : public Person {
private:
int rollNumber;
public:
Student(const std::string& n, int roll) : Person(n),
rollNumber(roll) {}
void displayInfo() {
Person::displayInfo();
std::cout << "Roll Number: " << rollNumber << std::endl;
}
};
int main() {
Person person("John");
person.displayInfo();
std::cout << std::endl;
Student student("Alice", 12345);
student.displayInfo();
return 0;
}
NOTE: tum simplicity ke liye normal message like im parent
class and im child class bhi dal sakte ho!!!
NOTE: ye baat sabhi inheritance ke upar kaam lagu karti hai
bus syntax ,mat badal na!!!
21| multi level inheritance.
#include <iostream>
#include <string>
using namespace std;
// Base class: Person
class Person {
protected:
string name;
public:
Person(const string& n) : name(n) {}
void displayInfo() {
cout << "Name: " << name << endl;
}
};
// Derived class: Student
class Student : public Person {
protected:
int rollNumber;
public:
Student(const string& n, int roll) : Person(n), rollNumber(roll)
{}
void displayInfo() {
Person::displayInfo();
cout << "Roll Number: " << rollNumber << endl;
}
};
// Derived class: UndergraduateStudent
class UndergraduateStudent : public Student {
private:
string major;
public:
UndergraduateStudent(const string& n, int roll, const string&
m)
: Student(n, roll), major(m) {}
void displayInfo() {
Student::displayInfo();
cout << "Major: " << major << endl;
}
};
int main() {
string name;
int rollNumber;
string major;
cout << "Enter person's name: ";
getline(cin, name);
cout << "Enter student's roll number: ";
cin >> rollNumber;
cin.ignore(); // Ignore the newline character
cout << "Enter undergraduate student's major: ";
getline(cin, major);
UndergraduateStudent uStudent(name, rollNumber, major);
uStudent.displayInfo();
return 0;
}
NOTE: again syntax same rakho, main function mai object
bana ke call karo!!!
22| multiple inheritance.
class parent1 {
Void get(){
cout<<”Hello im parent class 1”;
}
};
class parent2 {
Void net(){
cout<<”Hello im parent class 2”;
}
};
class child : public parent1, public parent2{
Void set(){
cout”Hello im the child class”;
}
};
Int main(){
obj child;
obj.get();
obj.net();
obj.set();
Return 0;
}
NOTE: here i made a simplified version of this, you can also
use this in the other inheritance program!!!
23| hierarchical inheritance.
class A{
void class1(){
cout “Hello im class A”;
}
};
class B : public A {
void class2(){
cout “Hello im class B”;
}
};
class C : public B {
void class3(){
cout “Hello im class C”;
}
};
Int main(){
obj C;
obj.class1();
obj.class2();
obj.class3();
Return 0;
}
Isme 23 tak hai which is hierarchical inheritance with the codes
which were old and repeatable removed, i will upload another
one around 12-01 which will have the code after these
Download