0% found this document useful (0 votes)
11 views15 pages

OOPS Lab Manual - I CS

The document outlines a series of C++ programming lab exercises focusing on Object-Oriented Programming concepts. It includes various programs demonstrating string manipulation, function overloading, virtual functions, static functions, file handling, operator overloading, and class constructors/destructors. Each program is accompanied by algorithms, code snippets, and expected outputs, providing a comprehensive guide for learning OOP in C++.

Uploaded by

gokulvarma985
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)
11 views15 pages

OOPS Lab Manual - I CS

The document outlines a series of C++ programming lab exercises focusing on Object-Oriented Programming concepts. It includes various programs demonstrating string manipulation, function overloading, virtual functions, static functions, file handling, operator overloading, and class constructors/destructors. Each program is accompanied by algorithms, code snippets, and expected outputs, providing a comprehensive guide for learning OOP in C++.

Uploaded by

gokulvarma985
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/ 15

XBC207 OBJECT ORIENTED PROGRAMMING LAB

UNIT-I
PROGRAM 1:
Number of vowels and number of characters in a string using c++ program

AIM:
To find the Number of vowels and number of characters in a string using c++ program

ALGORITHM:
1. Initialize 'vowelCount' and 'charCount' to zero.
2. For each character 'ch' in 'inputString', repeat steps 3-5.
a. If 'ch' is an alphabet (check using isalpha() function) then,
i. Increment 'charCount' by 1.
ii. Convert 'ch' to lowercase using tolower() function.
iii. If 'ch' is 'a', 'e', 'i', 'o', or 'u', then increment 'vowelCount' by 1.
3. Output 'vowelCount' as the number of vowels in the string.
4. Output 'charCount' as the number of characters in the string.

PROGRAM:
#include <iostream.h>
#include <conio.h>

int main()
{ char str[100];
int count = 0;
cout << "Enter a string to test :" << endl;
cin.get(str, 100);
for (int i = 0; str[i] != '\0'; i++)
{
switch (str[i])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout++;
}
}
cout << "Total number of vowels found : " << count << endl;
getch();
}

Ouptput:
Enter a string: Hello, World!
Input String: Hello, World!
Number of vowels: 3
Number of characters: 10

PROGRAM 2:
Write a function called zerosmaller () that is passed with two introduce arguments by
reference and set the smaller of the number to zero. Write a man() program to access this
function.

AIM:
To aim a function called zerosmaller () that is passed with two introduce arguments by
reference and set the smaller of the number to zero and a man() program to access this
function.

ALGORITHM:

1. The `zerosmaller` function is defined to take two integers by reference (`int &num1,
int &num2`).

2. Inside the `zerosmaller` function, it checks if `num1` is less than `num2`. If true, it sets
`num1` to 0; otherwise, it sets `num2` to 0.

3. In the `main` function, two integer variables (`number1` and `number2`) are declared.

4. The user is prompted to enter the values for `number1` and `number2` using `std::cin`.

5. The `zerosmaller` function is called with the two input numbers as arguments.

6. Finally, the program prints the modified numbers after calling the `zerosmaller`
function.
7. Run this program, it will take two numbers as input, and after calling the `zerosmaller`
function, it will display the modified numbers with the smaller one set to zero.
PROGRAM:
#include <iostream.h> #include
<conio.h> void zeroSmaller(int &num1,
int &num2)
{ if (num1 < num2)
{ num1 = 0;
} else
{ num2 = 0;
}
}

int main()
{ int a, b;

cout << "Enter the first number: ";


cin >> a;

cout << "Enter the second number: "; cin >> b; cout << "Original values: " <<
"num1 = " << a << ", num2 = " << b << std::endl;

zeroSmaller(a, b);

std::cout << "After zeroing the smaller number: " << "num1 = " << a << ",
num2 = " << b << std::endl;
getch(); return 0;
}

Output:
Enter the first number: 8
Enter the second number: 5
Original values: num1 = 8, num2 = 5
After zeroing the smaller number: num1 = 8, num2 = 0
UNIT-II
PROGRAM 3:
Demonstration of array of object.

AIM:
To demonstration of array of object using c++ coding.
ALGORITHM:
1. **Define a class:**
- Create a class with attributes and methods that represent the properties and behaviors
of the objects you want to store in the array.

2. **Declare an array of objects:**


- Declare an array to store objects of the class.

3. **Populate the array:** - For each index in the array:


- Create an object of the class.
- Set its properties using user input or predefined values. - Add the object to the array.

4. **Perform operations on objects in the array:** - For each object in the array:
- Call methods to perform specific operations on the objects.

5. **Display results:**
- Print or display the results of operations performed on objects in the array.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class Employee
{
int id; char
name[30];
public: void
getdata(); void
putdata();
};
void Employee::getdata()
{ cout<<"Enter Id :
"; cin>>id;
cout<<"Enter
Name : ";
cin>>name; }
void Employee::putdata()
{ cout<<id<<" ";
cout<<name<<" ";
cout<<endl; } int
main(){
Employee emp;
emp.getdata()
;
emp.putdata()
; getch();
return 0;

Output:
Enter Id : 101
Enter Name : aaa
101 aaa

PROGRAM 4:
Using this pointer to return a value (return by reference).

AIM:
To use the this pointer to return a value using return by reference.

ALGORITHM:

1. **Define a class:**
- Create a class with attributes and methods.

2. **Implement a method using `this` pointer:**


- Create a method that returns a value by reference using the `this` pointer.

3. Print or display the results of ‘this’ pointer.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class Employee {
public: int id;
float salary;
Employee(int id, float salary)
{ this->id = id;
this->salary = salary;
}
void display()
{ cout<<id<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101,890000);
Employee e2=Employee(102, 59000);
e1.display();
e2.display();
getch();
return 0;
}

Output:
101 890000
102 59000

UNIT-III
PROGRAM 5:
Demonstration of virtual function.

AIM:
To demonstrate the virtual function using c++ coding.

ALGORITHM:
• Virtual functions ensure that the correct function is called for an object, regardless
of the type of reference (or pointer) used for the function call.
• They are mainly used to achieve Runtime polymorphism.
• Functions are declared with a virtual keyword in a base class.
• The resolving of a function call is done at runtime.
• Optionally, call the non-virtual function to show that it retains its original
behavior.

PROGRAM:
#include<iostream.h
> #include<conio.h>
class base { public:
virtual void print() { cout << "print base class\n"; }
void show() { cout << "show base class\n"; }
}; class derived : public
base { public:
void print() { cout << "print derived class\n"; }
void show() { cout << "show derived class\n"; }
}; int
main() {
base* bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
getch();
return 0;
}

Output:
print derived class
show base class

PROGRAM 6:
Demonstration of static function

AIM:
To demonstrate the static function using c++ codings.

ALGORITHM:
● The static keyword is used with a variable to make the memory of the variable static
once a static variable is declared its memory can’t be changed.
● Static members of a class are not associated with the objects of the class. Just like a
static variable once declared is allocated with memory that can’t be changed every
object points to the same memory.

PROGRAM:
#include<iostream.h
> #include<conio.h>
class Student {
public: static int total;
Student() { total += 1; }
};
int Student::total =
0; int main() {
Student s1;
cout << "Number of students:" << s1.total << endl;
Student s2;
cout << "Number of students:" << s2.total << endl;
Student s3; cout << "Number of students:" <<
s3.total << endl; getch();
return 0;
}

Output:
Number of students:1
Number of students:2
Number of students:3

UNIT-IV
PROGRAM 7:
Accessing a particular record in a student's file.

AIM:
To access a particular record in a students file using c++ coding.

ALGORITHM:
1. Open the student file in the desired mode (read mode).
2. Prompt the user to enter the enrollment number or any unique identifier for the student
record they want to access.
3. Read the user input for the enrollment number or unique identifier.
4. While there are records in the file:
a. Read the next record from the file.
b.Check if the enrollment number or unique identifier in the record matches the user
input.
c. If there is a match, display the details of the student record (name, enrollment
number, department, program, etc.).
d.If there is no match, continue to the next record.
5. If the end of the file is reached and no matching record is found, display a message
indicating that the record was not found.
6. Close the file.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
main()
{ int rno,fee; char
name[50];
cout<<"Enter the Roll Number:";
cin>>rno;

cout<<"\nEnter the Name:";


cin>>name;

cout<<"\nEnter the Fee:";


cin>>fee; ofstream

fout("d:/student.doc");

fout<<rno<<"\t"<<name<<"\t"<<fee; //write data to the file student fout.close();

ifstream fin("d:/student.doc");
fin>>rno>>name>>fee; //read data from the file student
fin.close();
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
getch();
return 0;
}
Output:
Enter the Roll Number:1
Enter the Name:a
Enter the Fee:12000
1 a 12000

PROGRAM 8:
Demonstration of operator overloading.

AIM:
To demonstrate the operator overloading using c++ coding.

ALGORITHM:
1. Create a class named `Complex` to represent complex numbers.
2. In the class definition:
a. Declare private member variables for the real and imaginary parts of the complex
number.
b. Declare a default constructor to initialize the complex number to (0, 0).
c. Declare a parameterized constructor to set values for the real and imaginary parts.
d. Overload the `+` operator as a member function to add two complex numbers.
e. Overload the `<<` operator as a friend function to display complex numbers.
3. In the main function:
a. Create two `Complex` objects.
b. Use the overloaded `+` operator to add the two complex numbers.
c. Display the result using the overloaded `<<` operator.

PROGRAM:
#include<iostream.h
> #include<conio.h>
class Complex {
private: int real,
imag;

public:
Complex(int r = 0, int i = 0)
{ real = r;
imag = i;
}

Complex operator+(Complex const& obj)


{
Complex res; res.real =
real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << '\n'; }
};

int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
getch();
}
Output:
12 + i9

UNIT-V
PROGRAM 9:
Write a program to create a database for students that contains Name, Enrolment no,
Department, Programme using Constructors, destructors, input and output functions ;
input and output for 10 people using different methods.

AIM:
Write a program to create a database for students that contains Name, Enrolment no,
Department, Programme using Constructors, destructors, input and output functions;
input and output for 10 people using different methods.

ALGORITHM:
1: start the program
2: create a class as student.
3: declare a integer variable as roll inside of the student class.
4: declare a char as name and the set the value as 25.
5: declare a char as addr and the set the value as 100.
6: declare a int as std.
7: declare a char as div.
8: declare a long variable as cont.
9: get the int.char and lon data inside of public
10: display the data by using void display
11: print as object is created
12: print as object is created
Step 13: get the student data as int, roll, nm,ad,dv long,
Step 14: assign the variable roll as rol
Step 15: copy the string name as nm and addr as ad.
Step 16: assign the variable std as st, div as dv and cont as ct.
Step 17: display the student data by void display
Step 18: print roll number, name, address ,standard, division and contact.
Step 19:get the student data as
5,"Gangadhar","Suman_Apt,Malad_West",9,'A',28845562 by using
stud. Step 19:display the student data. Step 20: print the roll number Step
21: stop the program.

PROGRAM:
#include<iostream>
#include<string.h>
#include<conio.h>
class student
{
int roll; char name[25]; char addr[100]; int
std; char div; long cont; public:
voidget_data(int,char*,char*,int,char,long);
void
display(void);
student();
~student(); };
student::student(void)
{
cout<<"object is created"<<endl;
}
student::~student(void)
{
cout<<"object is destroyed!!!"<<endl;
}
void student::get_data(int rol,char *nm,char *ad,intst,chardv,longct)
{
roll=rol;
strcpy(name,nm);
strcpy(addr,ad); std=st;
div=dv;
cont=ct;
}
void student::display(void)
{ cout<<endl; cout<<"Roll :
"<<roll<<endl; cout<<"Name :
"<<name<<endl; cout<<"Address
: "<<addr<<endl; cout<<"Std :
"<<std<<endl; cout<<"Div :
"<<div<<endl; cout<<"Contact :
"<<cont<<endl; cout<<endl; }

int main()
{

student stud;
stud.get_data(5,"Gangadhar","Suman_Apt,Malad_West",9,'A',28845562);
stud.display();
cout<<“Roll No. =
“<<student::roll<<endl; return 0; }

OUTPUT:
Object is created
Roll :6
Name :Harish
Address :Vallam,PMIST
Std :bsc
Div:A
Contact :576909
object is destroyed!!!

PROGRAM 10:
Create a class holding information of the salaries of all the family members (husband,
wife, son, daughter). Using friend functions give the total salary of the family.

AIM:
To create a class holding information of the salaries of all the family members (husband,
wife, son, daughter). Using friend functions give the total salary of the family.
ALGORITHM:
1. Create a class named Family with private member variables for the salaries of
husband, wife, son, and daughter.
2. Define a friend function named `CalculateTotalSalary` outside the class to calculate
the total family salary.
3. In the class definition:
a. Declare the friend function `CalculateTotalSalary`.
b. Declare private member variables for husband, wife, son, and daughter salaries.
c. Define a default constructor to initialize member variables to default values.
d. Define a parameterized constructor to set values for husband, wife, son, and
daughter salaries.
4. In the friend function `CalculateTotalSalary`:
a. Take Family object as a parameter.
b. Sum up the salaries of husband, wife, son, and daughter.
c. Display the total family salary.
5. In the main function:
a. Create a Family object.
b. Input salaries for husband, wife, son, and daughter.
c. Call the friend function `CalculateTotalSalary` to display the total family salary.

PROGRAM:
#include
<iostream.h>
#include<conio.h>
class emp2; class
emp1 {
private:
int empno;
char
name[20];
float sal;
public: void
read(); void
show();
friend void cal(emp1 A,emp2 B);
};
class emp2
{
private:
int empno;
char name[20];
float sal; public:
void read();
void show();
friend void cal(emp1 A,emp2 B);
};
void emp1::read()
{ cout<<"Enter empno,name and sal
"; cin>>empno>>name>>sal;
}
void emp1::show()
{ cout<<"empno "<<empno<<" name "<<name<<" sal
"<<sal<<endl;
}
void emp2::read()
{ cout<<"Enter empno,name and sal
"; cin>>empno>>name>>sal;
}
void emp2::show()
{
cout<<"empno "<<empno<<" name "<<name<<" sal "<<sal<<endl;
}
void cal(emp1 A,emp2 B)
{
float t;
t=A.sal+B.sal; cout<<"Sum of
sal "<<t<<endl; cout<<"avg sal
"<<t/2<<endl;
}

int main()
{ emp1 e1;
emp2 e2;
e1.read(); e2.read();
e1.show();
e2.show();
cal(e1,e2); return(0);
getch();
}
Output:
Enter empno, name and sal 1001 aaa 45000
Enter empno, name and sal 1002 bbb 55000
Empno 1001 name aaa sal 45000
Empno 1002 name bbb sal 55000
Sum of sal 100000
Avg sal 50000

You might also like