0% found this document useful (0 votes)
9 views40 pages

C Lab

Annamalai University c++ lab manual

Uploaded by

shobadhas
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)
9 views40 pages

C Lab

Annamalai University c++ lab manual

Uploaded by

shobadhas
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/ 40

1. Function overloading.

#include <iostream>
using namespace std;
// function with 2 parameters
void display(int var1, double var2) {
cout<< "Integer number: " << var1;
cout<< " and double number: " << var2 <<endl;
}
// function with double type single parameter
void display(double var) {
cout<< "Double number: " << var <<endl;
}
// function with int type single parameter
void display(int var) {
cout<< "Integer number: " << var <<endl;
}
int main() {
int a = 5;
double b = 5.5;
// call function with int type parameter
display(a);
// call function with double type parameter
display(b);
// call function with 2 parameters
display(a, b);
return 0;
}
Output
Integer number: 5
Double number: 5.5
Integer number: 5 and double number: 5.5
2. // Class AndObjects
#include <iostream>
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height; }
};
int main() {
// create object of Room class
Room room1;
// assign values to data members
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
// calculate and display the area and volume of the room
cout<< "Area of Room = "<< room1.calculateArea() <<endl;
cout<< "Volume of Room = "<< room1.calculateVolume() <<endl;
return 0;
}
Output
Area of Room = 1309
Volume of Room = 25132.8
3. Passing object to functions.

// C++ program to calculate the average marks of two students

#include <iostream>
using namespace std;
class Student {
public:
double marks;
// constructor to initialize marks
Student(double m) {
marks = m;
}
};
// function that has objects as parameters
void calculateAverage(Student s1, Student s2) {
// calculate the average of marks of s1 and s2
double average = (s1.marks + s2.marks) / 2;
cout<< "Average Marks = " << average <<endl;
}
int main() {
Student student1(88.0), student2(56.0);
// pass the objects as arguments
calculateAverage(student1, student2);
return 0;
}
Output

Average Marks = 72
4. Friend function

#include <iostream>
using namespace std;

class Distance {
private:
int meter;

// friend function
friend int addFive(Distance);

public:
Distance() : meter(0) {}
};
// friend function definition
int addFive(Distance d) {

//accessing private members from the friend function


d.meter += 5;
return d.meter;
}

int main() {
Distance D;
cout<< "Distance: " <<addFive(D);
return 0;
}
Output

Distance: 5
5. Constructor and destructor
#include <iostream>
using namespace std;
class Department {
public:
Department() {
// Constructor is defined.
cout<< "Constructor Invoked for Department class" <<endl;
}
~Department() {
// Destructor is defined.
cout<< "Destructor Invoked for Department class" <<endl;
}
};
class Employee {
public:
Employee() {
// Constructor is defined.
cout<< "Constructor Invoked for Employee class" <<endl;
}
~Employee() {
// Destructor is defined.
cout<< "Destructor Invoked for Employee class" <<endl;
}};
int main(void) {
// Creating an object of Department.
Department d1;
// Creating an object of Employee.
Employee e2;
return 0;
}
Output
Constructor Invoked for Department class
Constructor Invoked for Employee class
Destructor Invoked for Employee class
Destructor Invoked for Department class
6. Unary operator overloading.
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ () {
++value;
}
void display() {
cout<< "Count: " << value <<endl;
}
};
int main() {
Count count1;
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
Output
Count: 6
7. Binary operator overloading.
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize real and imag to 0
Complex() : real(0), imag(0) {}
void input() {
cout<< "Enter real and imaginary parts respectively: ";
cin>> real;
cin>>imag;
}
// Overload the + operator
Complex operator + (const Complex& obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
void output() {
if (imag< 0)
cout<< "Output Complex number: " << real <<imag<< "i";
else
cout<< "Output Complex number: " << real << "+" <<imag<< "i";
}
};
int main() {
Complex complex1, complex2, result;

cout<< "Enter first complex number:\n";


complex1.input();

cout<< "Enter second complex number:\n";


complex2.input();

// complex1 calls the operator function


// complex2 is passed as an argument to the function
result = complex1 + complex2;
result.output();
return 0;
}
OUTPUT
Enter first complex number:
Enter real and imaginary parts respectively: 9 5
Enter second complex number:
Enter real and imaginary parts respectively: 7 6
Output Complex number: 16+11i
8. a. Single Inheritance
#include <iostream>
using namespace std;
// base class
class Animal {
public:
void eat() {
cout<< "I can eat!" <<endl;
}
void sleep() {
cout<< "I can sleep!" <<endl;
}
};
// derived class
class Dog : public Animal {
public:
void bark() {
cout<< "I can bark! Woof woof!!" <<endl;
}
};

int main() {
// Create object of the Dog class
Dog dog1;
// Calling members of the base class
dog1.eat();
dog1.sleep();
// Calling member of the derived class
dog1.bark();
return 0;
}
Output
I can eat!
I can sleep!
I can bark! Woof woof!!
8. b. Multiple Inheritance
#include <iostream>
using namespace std;
class Mammal {
public:
Mammal() {
cout<< "Mammals can give direct birth." <<endl;
}
};
class WingedAnimal {
public:
WingedAnimal() {
cout<< "Winged animal can flap." <<endl;
}
};
class Bat: public Mammal, public WingedAnimal {};
int main() {
Bat b1;
return 0;
}
Output
Mammals can give direct birth.
Winged animal can flap.
8. c. Multilevel Inheritance
#include <iostream>
using namespace std;
class base //single base class
{ public:
int x;
void getdata()
{
cout<< "Enter value of x= "; cin>> x;
} };
class derive1 : public base // derived class from base class
{ public:
int y;
void readdata()
{
cout<< "\nEnter value of y= "; cin>> y;
}};
class derive2 : public derive1 // derived from class derive1
{ private:
int z;
public:
void indata()
{ cout<< "\nEnter value of z= "; cin>> z;
}
void product()
{ cout<< "\nProduct= " << x * y * z;
} };
int main()
{
derive2 a; //object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();
return 0; }
Output
Enter value of x= 2
Enter value of y= 3
Enter value of z= 3
Product= 18
8. d.C++ program to demonstrate hierarchical inheritance
#include <iostream>
using namespace std;
// base class
class Animal {
public:
void info() {
cout<< "I am an animal." <<endl;
}
};
// derived class 1
class Dog : public Animal {
public:
void bark() {
cout<< "I am a Dog. Woof woof." <<endl;
} };
// derived class 2
class Cat : public Animal {
public:
void meow() {
cout<< "I am a Cat. Meow." <<endl;
} };
int main() {
// Create object of Dog class
Dog dog1;
cout<< "Dog Class:" <<endl;
dog1.info(); // Parent Class function
dog1.bark();
// Create object of Cat class
Cat cat1;
cout<< "\nCat Class:" <<endl;
cat1.info(); // Parent Class function
cat1.meow();
return 0;
}
Output

Dog Class:
I am an animal.
I am a Dog. Woof woof.

Cat Class:
I am an animal.
I am a Cat. Meow.
9. virtual function

#include <iostream>
using namespace std;

class Base {
public:
virtual void print() {
cout<< "Base Function" <<endl;
}
};

class Derived : public Base {


public:
void print() {
cout<< "Derived Function" <<endl;
}
};
int main() {
Derived derived1;

// pointer of Base type that points to derived1


Base* base1 = &derived1;

// calls member function of Derived class


base1->print();
return 0;
}
Output
Derived Function
10. C++ program to manipulate a text file.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// 1. write to a text file
fstreammy_file("example.txt", ios::out);
if (my_file) {
my_file<< "This is a test line." <<endl;
my_file.close();
}
else {
cout<< "Unable to open file for writing." <<endl;
return 1;
}
// 2. read from the same file
string line;
my_file.open("example.txt", ios::in);
if (my_file) {
while (!my_file.eof()) {
getline(my_file, line);
cout<< "Read from file: " << line <<endl;
}
my_file.close();
}
else {
cout<< "Unable to open file for reading." <<endl;
return 1;
}
// 3. append data to the end of the file
my_file.open("example.txt", ios::app);

if (my_file) {
my_file<< "This is another test line, appended to the file." <<endl;
my_file.close();
}
else {
cout<< "Unable to open file for appending." <<endl;
return 1;
}

return 0;
}
Output
Read from file: This is a test line.
Read from file:
11. Sequential I/O operations on a file
#include <iostream>
#include <fstream>
int main() {
// Writing data sequentially
std::ofstreamoutfile("data.txt");
if (outfile.is_open()) {
outfile<< "Record 1\n";
outfile<< "Record 2\n";
outfile<< "Record 3\n";
outfile.close();
}
// Reading data sequentially
std::ifstreaminfile("data.txt");
if (infile.is_open()) {
std::string line;
while (std::getline(infile, line)) {
std::cout<< line << std::endl;
}
infile.close();
}
return 0;
}
Output
Record 1
Record 2
Record 3
12. //Example for command line argument
#include<iostream.h>
#include<conio.h>
void main(int argc, char* argv[])
{
clrscr();
cout<<"\n Program name : \n"<<argv[0];
cout<<"1st arg :\n"<<argv[1];
cout<<"2nd arg :\n"<<argv[2];
cout<<"3rd arg :\n"<<argv[3];
cout<<"4th arg :\n"<<argv[4];
cout<<"5th arg :\n"<<argv[5];
getch();
}
Output
C:/TC/BIN>TCC mycmd.cpp
C:/TC/BIN>mycmd this is a program
Program name : c:/tc/bin/mycmd.cpp
1st arg : this
2nd arg : is
3rd arg : a
4th arg : program
5th arg : (null)
13. // C++ program to demonstrate the use of class templates

#include <iostream>
using namespace std;
// Class template
template <class T>
class Number {
private:
// Variable of type T
T num;
public:
Number(T n) : num(n) {} // constructor

T getNum() {
return num;
}
};
int main() {
// create object with int type
Number<int>numberInt(7);
// create object with double type
Number<double>numberDouble(7.7);
cout<< "int Number = " <<numberInt.getNum() <<endl;
cout<< "double Number = " <<numberDouble.getNum() <<endl;
return 0;
}
Output
int Number = 7
double Number = 7.7
14. C++ program to demonstrate function template

#include <iostream>
using namespace std;
template <typename T>
T add(T num1, T num2) {
return (num1 + num2);
}
int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout<< "2 + 3 = " << result1 <<endl;
// calling with double parameters
result2 = add<double>(2.2, 3.3);
cout<< "2.2 + 3.3 = " << result2 <<endl;
return 0;
}
Output
2+3=5
2.2 + 3.3 = 5.5
15. Exception Handling
// program to divide two numbers
// throws an exception when the divisor is 0
#include <iostream>
using namespace std;
int main() {
double numerator, denominator, divide;
cout<< "Enter numerator: ";
cin>> numerator;
cout<< "Enter denominator: ";
cin>> denominator;
try {
// throw an exception if denominator is 0
if (denominator == 0)
throw 0;
// not executed if denominator is 0
divide = numerator / denominator;
cout<< numerator << " / " << denominator << " = " << divide <<endl;
}
catch (int num_exception) {
cout<< "Error: Cannot divide by " <<num_exception<<endl;
}
return 0;
}
Output
Enter numerator: 72
Enter denominator: 0
ERROR!
Error: Cannot divide by 0
IMMACULATE COLLEGE FOR WOMEN-VIRIYUR
DEPARTRMENT OF BCA
RECORD NOTE BOOK

Name : ------------------------------
Course : -----------------------------
Reg. No: -----------------------------
Year : ------------------------------

CERTIFICATE

This is to certify that ----------------------------of ---------------------- Has been


appearing for the University Practical examination of “ OBJECT ORIENTED
USING C++ LAB “and record work done by her in the computer laboratory
during the academic year -------------.

Staff-in-charge Head Of the Department

Submitted on-------------------- for the practical examination held on ----------

1. Internal Examiner 2.External Examiner


CONTENT

S.NO DATE PARTICULARS PAGE SIGNATURE


NO

1 FUNCTION OVERLOADING

2 CLASS AND OBJECTS

3 PASSING OBJECTS TO FUNCTIONS

4 FRIEND FUNCTIONS

5 CONSTRUCTOR AND DESTRUCTOR

6 UNARY OPERATOR OVERLOADING

7 BINARY OPERATOR OVERLOADING


 SINGLE INHERITANCE
8
 MULTILEVEL INHERITANCE
 MULTIPLE INHERITANCE
 HIERARCHICAL
INHERITANCE

9 VIRTUAL FUNCTIONS

10 TEXT FILE

11 SEQUENTIAL I/O OPERATIONS ON A


FILE

12 COMMAND LINE ARGUMENTS

13 CLASS TEMPLATE

14 FUNCTION TEMPLATE

15 EXCEPTION HANDLING

You might also like