0% found this document useful (0 votes)
3 views5 pages

C++ Programming Basics and Examples

Uploaded by

mrattanbe23
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)
3 views5 pages

C++ Programming Basics and Examples

Uploaded by

mrattanbe23
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

#include <iostream>

using namespace std;

int main() {
// Q1
cout << "Q1. Escape Sequences Output:\n";
cout << "Hello World\n";
cout << "This is a new line\nThis is the second Line" << endl;
cout << "This is with\ttab space" << endl;
cout << "This will make alarm sound\a" << endl;
cout << "Hello World!\rStart" << endl << endl;

// Q2: Celsius to Fahrenheit


float celsius, fahrenheit;
cout << "Q2. Celsius to Fahrenheit:\n";
cout << "Enter temperature in Celsius: ";
cin >> celsius;
fahrenheit = (9 * celsius) / 5 + 32;
cout << "Temperature in Fahrenheit: " << fahrenheit << endl << endl;

// Q3: Loop Demonstrations

cout << "1. for loop (standard format): ";


for (int i = 0; i < 5; i++) {
cout << i << " ";
}
cout << endl;

cout << "2. for loop (without initialization): ";


int i = 0;
for (; i < 5; i++) {
cout << i << " ";
}
cout << endl;

cout << "3. for loop (without increment): ";


i = 0;
for (; i < 5;) {
cout << i << " ";
i++;
}
cout << endl;

cout << "4. for loop (only condition): ";


i = 0;
for (; i < 5;) {
cout << i << " ";
i++;
}
cout << endl;

cout << "5. while loop: ";


i = 0;
while (i < 5) {
cout << i << " ";
i++;
}
cout << endl;

cout << "6. do while loop (runs at least once): ";


int n = 0;
do {
cout << n << " ";
n++;
} while (n < 5);
cout << endl << endl;

// Q4: Call Student Structure Demo


cout << "Q4. Student Structure Demo:\n";

// Student object and function calls


struct Student {
string name, degree, hostel;
int rollNo;
float cgpa;

void add() {
cout << "Enter Name, Roll No, Degree, Hostel, CGPA: ";
cin >> name >> rollNo >> degree >> hostel >> cgpa;
}

void update() {
cout << "Update Name and Degree: ";
cin >> name >> degree;
}

void updateCGPA() {
cout << "Update CGPA: ";
cin >> cgpa;
}

void updateHostel() {
cout << "Update Hostel: ";
cin >> hostel;
}
void show() {
cout << "\nStudent Info:\n";
cout << "Name: " << name << "\nRoll No: " << rollNo << "\nDegree: "
<< degree;
cout << "\nHostel: " << hostel << "\nCGPA: " << cgpa << endl;
}
};

Student s;
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();

// Q5: Class with private/public access modifiers

class StudentClass {
private:
string name, degree, hostel;
int rollNo;
float cgpa;

void privateShow() {
cout << "Private Function Accessed!" << endl;
}

public:
void add() {
cout << "Enter Name, Roll No, Degree, Hostel, CGPA: ";
cin >> name >> rollNo >> degree >> hostel >> cgpa;
}

void show() {
privateShow(); // Accessing private method
cout << "\nStudent Info (Class):\n";
cout << "Name: " << name << "\nRoll No: " << rollNo << "\nDegree: "
<< degree;
cout << "\nHostel: " << hostel << "\nCGPA: " << cgpa << endl;
}
};
StudentClass s2;
[Link]();
[Link]();

// Q6: Calling private member functions inside public functions


cout << "\nQ6. Private Function Access Inside Public Function:\n";

class Demo {
private:
void secretMessage() {
cout << "Accessed Private Function from Public Function.\n";
}

public:
void reveal() {
cout << "Calling private function...\n";
secretMessage(); // Legal: called from inside class
}
};

Demo d;
[Link]();

// Q7: Complex Number Class


cout << "\nQ7. Complex Number Operations:\n";

class Complex {
private:
float real, imaginary;

public:
void set() {
cout << "Enter real and imaginary parts: ";
cin >> real >> imaginary;
}

void display() {
cout << "Complex Number: " << real << " + " << imaginary << "i" <<
endl;
}

Complex sum(Complex c) {
Complex temp;
[Link] = real + [Link];
[Link] = imaginary + [Link];
return temp;
}
};

Complex c1, c2, c3;


cout << "Enter first complex number:\n";
[Link]();
cout << "Enter second complex number:\n";
[Link]();
c3 = [Link](c2);
cout << "Sum is:\n";
[Link]();

// Q8: Namespaces

namespace FirstLib {
int value = 10;
void print() {
cout << "FirstLib Value: " << value << endl;
}
}

namespace SecondLib {
int value = 20;
void print() {
cout << "SecondLib Value: " << value << endl;
}
}

FirstLib::print();
SecondLib::print();

return 0;
}

You might also like