0% found this document useful (0 votes)
12 views23 pages

C++ Practical Report

The document outlines various C++ programming experiments, including swapping numbers, calculating sums with default arguments, finding factorials, and demonstrating inheritance. Each experiment includes a brief aim, code snippets, and expected outputs. The document also covers advanced topics such as operator overloading, virtual destructors, and file handling.
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)
12 views23 pages

C++ Practical Report

The document outlines various C++ programming experiments, including swapping numbers, calculating sums with default arguments, finding factorials, and demonstrating inheritance. Each experiment includes a brief aim, code snippets, and expected outputs. The document also covers advanced topics such as operator overloading, virtual destructors, and file handling.
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/ 23

Aim Of The Experiment

Write a Program for Swapping of two numbers.


Program Code:
#include <iostream>
using namespace std;
int main(){
int a,b;
cout<<"enter first number: ";
cin>>a;
cout<<"enter the second number: ";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"after swapping:\n";
cout<<"first number= "<<a<<"\n";
cout<<"second number= "<<b;
return 0;
}
Output:
Aim Of The Experiment:
Write a Program to find sum off our numbers using default argument passing.
Program Code:
#include <iostream>
using namespace std;
void add(int a, int b=0, int c=0, int d=0){
cout<<a+b+c+d<<"\n";
}
int main(){
add(10,20,30,40);
add(10,20,30);
add(10,20);
add(10);
return 0;
}
Output:
Aim Of The Experiment:
Write a Program to find square and cube of a number using inline function.
Program Code:
#include <iostream>
using namespace std;
inline int square(int n){
return n*n;
}
inline int cube(int n){
return n*n*n;
}
int main(){
int number;
cout<<"enter a number: ";
cin>>number;
cout<<"square of"<<number<<"is: "<<square(number)<<endl;
cout<<"cube of"<<number<<"is: "<<cube(number)<<endl;
return 0;
}
Output:
Aim Of The Experiment:
Write a Program to find the factorial of a number.
Program Code:
#include <iostream>
using namespace std;
int main(){
int num;
unsigned long long factorial=1;
cout<<"enter a posituve integer: ";
cin>>num;
if(num<0){
cout<<"factorial is not defined for negative numbers."<<endl;
}else{
for(int i=1;i<=num;++i){
factorial*=i;
}
cout<<"factorial of "<<num<<"= "<<factorial<<endl;
}
return 0;
}
Output:
Aim Of The Experiment:
Write a Program to find reverse of a number.
Program Code:
#include <iostream>
using namespace std;
int main(){
int number,reverse=0;
cout<<"enter a number: ";
cin>>number;
int original=number;
while(number!=0){
int digit=number%10;
reverse=reverse*10+digit;
number/=10;
}
cout<<"reverse of "<<original<<" is: "<<reverse<<endl;
return 0;
}
Output:
Aim Of The Experiment:
Write a program to find sum off our numbers using default argument passing
inmember function.
Program Code:
#include <iostream>
using namespace std;
class sumcalculator{
public:
int sum(int a, int b, int c=0, int d=0){
return a+b+c+d;
}
};
int main(){
sumcalculator calc;
int result1=calc.sum(10,20);
int result2=calc.sum(10,20,30,40);
int result3=calc.sum(10,20,30);
cout<<"sum of 10 and 20:"<<result1<<endl;
cout<<"sum of 10,20 and 30:"<<result3<<endl;
cout<<"sum of 10,20,30 and 40:"<<result2<<endl;
return 0;
}
Output:
Aim Of The Experiment:
Write a Program to find area of circle , triangle and rectangle using function overloading.
Program Code:
#include <iostream>
using namespace std;
class areacalculator{
public:
float area(float radius){
return 3.14159f*radius*radius;
}
float area(float length, float breadth){
return length*breadth;
}
float area(float base, float height, int flag){
return 0.5*base*height;
}
};
int main(){
areacalculator calc;
float r=5.0;
float l=10.0,b=4.0;
float base=6.0,height=3.0;
cout<<"area of triangle(r="<<r<<")="<<calc.area(r)<<endl;
cout<<"area of rectangle(l="<<l<<",b="<<b<<")="<<calc.area(l,b)<<endl;
cout<<"area of
triangle(base="<<base<<",height="<<height<<")="<<calc.area(base,height,1)<<endl;
return 0;
}
Output:
Aim Of The Experiment:
Write a program to distinguish the properties of static and non-static data members.
Program Code:
#include <iostream>
using namespace std;
class counter{
private:
int nonstaticcount;
static int staticcount;
public:
counter(){
nonstaticcount=0;
}
void increment(){
nonstaticcount++;
staticcount++;
}
void display(){
cout<<"non-static count: "<<nonstaticcount<<endl;
cout<<"static count: "<<staticcount<<endl;
}
};
int counter::staticcount=0;
int main(){
counter obj1,obj2;
cout<<"calling increment() on obj1 three times:\n";
obj1.increment();
obj1.increment();
obj1.increment();
obj1.display();
cout<<"\ncalling increment() on obj2 two times:\n";
obj2.increment();
obj2.increment();
obj2.display();
cout<<"\ndisplaying obj1 again:\n";
obj1.display();
return 0;
}
Output:
Aim Of The Experiment:
Write a program to show the method of accessing static private member function.
Program Code:
#include <iostream>
using namespace std;
class Demo {
private:
static void privateStaticFunction() {
cout << "Private Static Function called!" << endl;
}
public:
static void accessPrivateFunction() {
cout << "Accessing private static function from public function...\n";
privateStaticFunction();
}
};
int main() {
Demo::accessPrivateFunction();
return 0;
}
Output:
Aim Of The Experiment:
Write a program to show the ways of calling constructors and destructors
Program Code:
#include <iostream>
using namespace std;
class Demo {
public:
Demo() {
cout << "Default Constructor called!" << endl;
}
Demo(int x) {
cout << "Parameterized Constructor called with value: " << x << endl;
}
Demo(const Demo &obj) {
cout << "Copy Constructor called!" << endl;
}
~Demo() {
cout << "Destructor called!" << endl;
}
};
int main() {
cout << "Creating object1 using Default Constructor:" << endl;
Demo object1;
cout << "\nCreating object2 using Parameterized Constructor:" << endl;
Demo object2(10);
cout << "\nCreating object3 using Copy Constructor (copying object2):" << endl;
Demo object3 = object2;
cout << "\nExiting main... Destructors will be called in reverse order of creation." << endl;
return 0;
}
Output:
Aim Of The Experiment:
Write a program to perform ++ operator overloading using member function.
Program Code:
#include <iostream>
using namespace std;
class Counter {
private:
int value;
public:
Counter(int v = 0) {
value = v;
}
Counter operator++() {
++value;
return *this;
}
void display() const {
cout << "Value: " << value << endl;
}
};
int main() {
Counter c1(5);
cout << "Before increment:" << endl;
c1.display();
++c1;
cout << "After prefix increment (++c1):" << endl;
c1.display();
return 0;
}
Output:
Aim Of The Experiment:
Write a program to perform ++ operator overloading using friend function.
Program Code:
#include <iostream>
using namespace std;
class Counter {
private:
int value;
public:
Counter(int v = 0) {
value = v;
}
friend Counter operator++(Counter &c);
void display() const {
cout << "Value: " << value << endl;
}
};
Counter operator++(Counter &c) {
++c.value;
return c;
}
int main() {
Counter c1(10);
cout << "Before increment:" << endl;
c1.display();
++c1;
cout << "After prefix increment (++c1):" << endl;
c1.display();
return 0;
}
Output:
Aim Of The Experiment:
Write a program to perform + operator overloading for two complex number addition.
Program Code:
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
Complex(float r = 0, float i = 0) {
real = r;
imag = i;
}
Complex operator+(const Complex &obj) {
Complex result;
result.real = real + obj.real;
result.imag = imag + obj.imag;
return result;
}
void display() const {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(3.5, 2.5);
Complex c2(1.5, 4.5);
cout << "First Complex Number: ";
c1.display();
cout << "Second Complex Number: ";
c2.display();
Complex sum = c1 + c2;
cout << "Sum of Complex Numbers: ";
sum.display();
return 0;
}
Output:
Aim Of The Experiment:
Write a program to perform + operator overloading for string concatenation.
Program Code:
#include <iostream>
using namespace std;
class MyString {
private:
string str;
public:
MyString(string s = "") {
str = s;
}
MyString operator+(const MyString &obj) {
return MyString(str + obj.str);
}
void display() const {
cout << str << endl;
}
};
int main() {
MyString s1("Hello, ");
MyString s2("World!");
cout << "First String: ";
s1.display();
cout << "Second String: ";
s2.display();
MyString s3 = s1 + s2;
cout << "Concatenated String: ";
s3.display();
return 0;
}
Output:
Aim Of The Experiment:
Write a program to perform single inheritance.
Program Code:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "The dog barks: Woof! Woof!" << endl;
}
};
int main() {
Dog myDog;
myDog.eat();
myDog.bark();
return 0;
}
Output:
Aim Of The Experiment:
Write a program to perform multiple inheritance.
Program Code:
#include <iostream>
using namespace std;
class Student {
public:
void displayStudent() {
cout << "I am a student." << endl;
}
};
class Athlete {
public:
void displayAthlete() {
cout << "I am an athlete." << endl;
}
};
class ScholarAthlete : public Student, public Athlete {
public:
void displayScholarAthlete() {
cout << "I am a scholar and an athlete!" << endl;
}
};
int main() {
ScholarAthlete person;
person.displayStudent();
person.displayAthlete();
person.displayScholarAthlete();
return 0;
}
Output:
Aim Of The Experiment:
Write a program to create an integer array using new operator and find the sum and average of array
elements.
Program Code:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int* arr = new int[n];
cout << "Enter " << n << " integers:" << endl;
for(int i = 0; i < n; i++) {
cout << "Element " << i + 1 << ": ";
cin >> arr[i];
}
int sum = 0;
for(int i = 0; i < n; i++) {
sum += arr[i];
}
float average = static_cast<float>(sum) / n;
cout << "\nSum of elements: " << sum << endl;
cout << "Average of elements: " << average << endl;
delete[] arr;
return 0;
}
Output:
Aim Of The Experiment:
Write a program to implement virtual destructor.
Program Code:
#include <iostream>
using namespace std;
class Base {
public:
Base() {
cout << "Base constructor called." << endl;
}
virtual ~Base() {
cout << "Base destructor called." << endl;
}
};
class Derived : public Base {
public:
Derived() {
cout << "Derived constructor called." << endl;
}
~Derived() {
cout << "Derived destructor called." << endl;
}
};
int main() {
Base* ptr = new Derived();
cout << "Deleting object through base pointer..." << endl;
delete ptr;
return 0;
}
Output:
Aim Of The Experiment:
Create the Person class. Create some objects of this class (by taking information fromthe user). Inherit the class
Person to create two classes Teacher and Student class.Maintain the respective information in the classes and
create , display and delete objects of these two classes(Use Runtime Polymorphism).
Program Code:
#include <iostream>
using namespace std;
class Person {
protected:
string name;
int age;
public:
virtual ~Person() {
cout << "Person destructor called." << endl;
}
virtual void input() {
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
}
virtual void display() const {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
class Student : public Person {
private:
string course;
int year;
public:
~Student() {
cout << "Student destructor called." << endl;
}
void input() override {
Person::input();
cout << "Enter course: ";
cin >> course;
cout << "Enter year: ";
cin >> year;
}
void display() const override {
Person::display();
cout << "Course: " << course << ", Year: " << year << endl;
}
};
class Teacher : public Person {
private:
string subject;
double salary;
public:
~Teacher() {
cout << "Teacher destructor called." << endl;
}
void input() override {
Person::input();
cout << "Enter subject: ";
cin >> subject;
cout << "Enter salary: ";
cin >> salary;
}
void display() const override {
Person::display();
cout << "Subject: " << subject << ", Salary: " << salary << endl;
}
};
int main() {
const int size = 2;
Person* people[size];
cout << "Enter details for " << size << " persons (students/teachers):" << endl;
for (int i = 0; i < size; i++) {
int choice;
cout << "\nEnter 1 for Student, 2 for Teacher: ";
cin >> choice;
if (choice == 1) {
people[i] = new Student();
} else if (choice == 2) {
people[i] = new Teacher();
} else {
cout << "Invalid choice. Skipping..." << endl;
people[i] = nullptr;
continue;
}
people[i]->input();
}
cout << "\n--- Displaying Information ---" << endl;
for (int i = 0; i < size; i++) {
if (people[i]) {
people[i]->display();
}
}
cout << "\n--- Deleting Objects ---" << endl;
for (int i = 0; i < size; i++) {
delete people[i];
}
return 0;
}
Output:
Aim Of The Experiment:
Write a program to Copy the contents of one file to other.
Program Code:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string sourceFile, destinationFile;
cout << "Enter source file name: ";
cin >> sourceFile;
cout << "Enter destination file name: ";
cin >> destinationFile;
ifstream inFile(sourceFile);
if (!inFile) {
cout << "Error: Cannot open source file!" << endl;
return 1;
}
ofstream outFile(destinationFile);
if (!outFile) {
cout << "Error: Cannot create destination file!" << endl;
return 1;
}
string line;
while (getline(inFile, line)) {
outFile << line << endl;
}
cout << "File copied successfully!" << endl;
inFile.close();
outFile.close();
return 0;
}
Output:

You might also like