Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
75 views25 pages

Oops List of Practicals

The document contains multiple C++ programming tasks and their corresponding source codes, covering topics such as class and struct declarations, memory allocation, inheritance, and function overloading. Each task includes a description of the functionality required and the implementation details in C++. The document serves as a practical guide for learning C++ programming concepts through hands-on examples.

Uploaded by

chetanvasantala
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)
75 views25 pages

Oops List of Practicals

The document contains multiple C++ programming tasks and their corresponding source codes, covering topics such as class and struct declarations, memory allocation, inheritance, and function overloading. Each task includes a description of the functionality required and the implementation details in C++. The document serves as a practical guide for learning C++ programming concepts through hands-on examples.

Uploaded by

chetanvasantala
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/ 25

LAB 5

1 Write a C++ program to display Names, Roll No., and grade of 3 students who
have appeared in the examination. Declare the class of name, roll no., and grade.
Create an array of class objects. Read and display the contents of the array.

Source Code:
#include<iostream>

using namespace std;

class student

string name;

char grade;

int roll_no;

public:

void getStu()

cout<<"Enter student name:";

cin>>name;

cout<<"\n Enter student grade:";

cin>>grade;

cout<<"\n Enter student roll no:";

cin>>roll_no;

void showStu()

cout<<"Student name is:"<<name<<"\n";

cout<<"Student roll no. is:"<<roll_no<<"\n";

cout<<"Student grade is:"<<grade<<"\n";

};

int main()

{
student stud[3];

for(int i=0;i<3;i++)

cout<<"For student "<<(i+1)<<"\n";

stud[i].getStu();

for(int i=0;i<3;i++)

cout<<"For student "<<(i+1)<<"\n";

stud[i].showStu();

return 0;

Output:
2 Write a C++ program to declare struct. Initialise and display contents of Member.
Source Code:
#include<iostream>

using namespace std;

struct vehicle

private:

string colour;

int price;

string max_speed;

public:

void getvehicle()

cout<<"Enter colour:";

cin>> colour;

cout<<"Enter price:";

cin>>price;

cout<<"Enter maximum speed:";

cin>>max_speed;

void showvehicle()

cout<<"Colour is:"<<colour<<"\n";

cout<<"Price is:"<<price<<"\n";

cout<<"maximum speed is:"<<max_speed<<"\n";

};

int main()

vehicle car[3];

for(int i=0;i<3;i++)
{

cout<<"For car "<<(i+1)<<"\n";

car[i].getvehicle();

for(int i=0;i<3;i++)

cout<<"For car "<<(i+1)<<"\n";

car[i].showvehicle();

return 0;

Output:
3. Write a C++ program to declare a class . Declare pointer to class. Initialise and
display the contents of the class members.
Source code:
#include<iostream>

using namespace std;

class Emp{

private:

int EmpId;

string EmpName;

float Salary;

public:

void input(void){

cout<<"Enter EmpId ";

cin>> EmpId;

cout<<endl<<"Enter EmpName ";

cin>> EmpName;

cout<<endl<<"Enter Salary ";

cin>> Salary;

void print(void){

cout<<endl<<"EmpId- "<<EmpId;

cout<<endl<<"Empname- "<<EmpName;

cout<<endl<<"Salary- "<<Salary;}

};

int main(){

Emp A;

Emp *z=&A;

z->input();

cout<<endl;

z->print();

}
Output:
4 . Given that EMPLOYEE class contains following members: data members:
Employee number, Employee name, Basic, DA,IT, Net Salary, and print data
members.
Source code:
#include <iostream>

#include <string>

using namespace std;

class EMPLOYEE {

private:

int EmployeeNumber;

string EmployeeName;

double Basic;

double DA;

double IT;

double NetSalary;

public:

EMPLOYEE(int empNumber, string empName, double basic, double da, double it) {

EmployeeNumber = empNumber;

EmployeeName = empName;

Basic = basic;

DA = da;

IT = it;

calculateNetSalary();

void calculateNetSalary() {

NetSalary = Basic + DA - IT;

void printDataMembers() {

cout << "Employee Number: " << EmployeeNumber << endl;

cout << "Employee Name: " << EmployeeName << endl;

cout << "Basic Salary: " << Basic << endl;


cout << "DA: " << DA << endl;

cout << "Income Tax: " << IT << endl;

cout << "Net Salary: " << NetSalary << endl;

};

int main() {

int empNumber;

string empName;

double basic, da, it;

cout << "Enter Employee Number: ";

cin >> empNumber;

cout << "Enter Employee Name: ";

cin>>empName;

cout << "Enter Basic Salary: ";

cin >> basic;

cout << "Enter DA: ";

cin >> da;

cout << "Enter Income Tax: ";

cin >> it;

EMPLOYEE emp(empNumber, empName, basic, da, it);

emp.printDataMembers();

return 0;

Output:
5 Write a C++ program to read the data of N employee and compute the net salary
of each employee (DA=52% of basic and IT=30% of gross salary).
Source code:
#include <iostream>

#include <string>

#include <vector>

using namespace std;

class Employee {

private:

int EmployeeNumber;

string EmployeeName;

double Basic;

double DA;

double IT;

double NetSalary;

public:

// Constructor to initialize the data members

Employee(int empNumber, string empName, double basic) {

EmployeeNumber = empNumber;

EmployeeName = empName;

Basic = basic;

DA = 0.52 * Basic; // DA is 52% of Basic

calculateNetSalary();

void calculateNetSalary() {

IT = 0.3 * (Basic + DA); // IT is 30% of Gross Salary

NetSalary = Basic + DA - IT;

void printDataMembers() const {

cout << "Employee Number: " << EmployeeNumber << endl;

cout << "Employee Name: " << EmployeeName << endl;


cout << "Basic Salary: " << Basic << endl;

cout << "DA: " << DA << endl;

cout << "Income Tax: " << IT << endl;

cout << "Net Salary: " << NetSalary << endl;

};

int main() {

int N;

cout << "Enter the number of employees: ";

cin >> N;

vector<Employee> employees;

for (int i = 0; i < N; i++) {

int empNumber;

string empName;

double basic;

cout << "Enter details for Employee " << i + 1 << ":" << endl;

cout << "Employee Number: ";

cin >> empNumber;

cout << "Employee Name: ";

cin>>empName;

cout << "Basic Salary: ";

cin >> basic;

Employee emp(empNumber, empName, basic);

employees.push_back(emp);

for (const Employee &emp : employees) {

emp.printDataMembers();

cout << "----------------------------------------" << endl;

return 0;

}
Output:
LAB 6
6 Write a C++ program to use scope resolution operator. Display the various
values of the same variables declared at different scope levels.
Source code:
#include <iostream>

using namespace std;

int globalVar = 10;

int main() {

int localVar = 20;

cout << "Global variable: " << globalVar << endl;

cout << "Local variable in main: " << localVar << endl;

int blockVar = 30;

cout << "Block-level variable: " << blockVar << endl;

cout << "Global variable inside block: " << ::globalVar << endl;

cout << "Global variable after the block: " << globalVar << endl;

return 0;

Output:
7 Write a C++ program to allocate memory using new operator.
Source code:
# include<iostream>

using namespace std;

class NEW{

private:

int marks;

string Name;

int rollno;

public:

void input(void);

void print(void);

};

void NEW:: input(void){

cout<<"Enter marks ";

cin>> marks;

cout<<"Enter Name ";

cin>> Name;

cout<<"Enter roll ";

cin>> rollno;

void NEW:: print(void){

cout<<endl<<"marks- "<<marks;

cout<<endl<<"name- "<<Name;

cout<<endl<<"rollno- "<<rollno;

int main(){

NEW *ptr;

ptr=new NEW;

ptr->input();

ptr->print();
}

Output:
8) Write a C++ program to create an array of pointers. Invoke functions using array
of objects.
Source code:
#include <iostream>

using namespace std;

class Student {

public:

string name;

int rollNo;

Student(string name1, int roll){

name=name1;

rollNo=roll; }

void displayInfo() {

cout << "Name: " << name << endl;

cout << "Roll Number: " << rollNo << endl; }

};

int main() {

int arraySize = 3;

Student* studentArray[arraySize];

studentArray[0] = new Student("Alice", 101);

studentArray[1] = new Student("Bob", 102);

studentArray[2] = new Student("Charlie", 103);

for (int i = 0; i < arraySize; ++i) {

cout << "Student " << i + 1 << " Information:\n";

studentArray[i]->displayInfo();

cout << endl; }

for (int i = 0; i < arraySize; ++i) {

delete studentArray[i];

}
Output:
9) Write a C++ program to create multilevel inheritance.
Source code:
#include <iostream>

#include <string>

using namespace std;

class Employee {

private:

string name;

int employeeId;

public:

Employee(){}

Employee(const string& _name, int _employeeId) : name(_name), employeeId(_employeeId) {}

void getdata()

cout<<"Enter name:";

cin>>name;

cout<<"Enter id";

cin>>employeeId;

void displayInfo() {

cout << "Name: " << name <<endl;

cout << "Employee ID: " << employeeId << endl;

};

class Programmer : public Employee {

private:

string programmingLanguage;
public:

Programmer():Employee(){}

Programmer(const string& _name, int _employeeId, const string& _programmingLanguage)

: Employee(_name, _employeeId), programmingLanguage(_programmingLanguage) {}

void getdata()

Employee::getdata();

cout<<"Enter Programming Language: ";

cin>>programmingLanguage;

void displayInfo() {

Employee::displayInfo();

cout << "Programming Language: " << programmingLanguage <<endl;

};

class Manager : public Employee {

private:

string department;

public:

Manager():Employee(){}

Manager(const string& _name, int _employeeId, const string& _department)

: Employee(_name, _employeeId), department(_department) {}

void getdata()

Employee::getdata();

cout<<"Enter Department: ";

cin>>department;
}

void displayInfo() {

Employee::displayInfo();

cout << "Department: " << department <<endl;

};

int main() {

Programmer programmer;

Manager manager;

programmer.getdata();

cout << "Programmer Information:" <<endl;

programmer.displayInfo();

manager.getdata();

cout << "\nManager Information:" <<endl;

manager.displayInfo();

return 0;

Output:
LAB 7
10) Create a class named Shape with a function that prints “This is a Shape”.Create
another class named Polygon inheriting the Shape class with the same function
that prints “Polygon is a shape”. Create two other classes named Rectangle and
Triangle having the same function which prints “Rectangle is a polygon” and
“Triangle is a polygon” respectively.Again, make another class named Square
having the same function which prints “Square is a rectangle”. Now try calling the
function by the object of each of these classes.

Source code:
#include<iostream>

using namespace std;

class Shape

public:

void display()

cout<<"This is a shape"<<endl;

};

class Polygon:public Shape

public:

void display()

cout<<"Polygon is shape"<<endl;

};

class rectangle:public Polygon

public:

void display()
{

cout<<"Rectangle is a Polygon"<<endl;

};

class triangle:public Polygon

public:

void display()

cout<<"Traiangle is a Polygon"<<endl;

};

class square:public rectangle

public:

void display()

cout<<"Square is a rectangle"<<endl;

};

int main()

Shape s;

Polygon p;

rectangle r;

triangle t;

square q;

s.display(); p.display(); r.display();

t.display();

q.display();

}
Output:
11) . Create a class fruits with a data member to count the number of fruits in a
basket, create two more classes apples and mangoes derived from the fruit class.
Create a member function to calculate number of each in the basket.
Source code:
#include <iostream>

using namespace std;

class Fruits {

protected:

int count;

public:

Fruits() : count(0) {}

void addToBasket(int num) {

count += num;

int getCount() const {

return count;

};

class Apples : public Fruits {

public:

void calculateApples() {

cout << "Enter the number of apples in the basket: ";

int numApples;

cin >> numApples;

addToBasket(numApples);

cout << "Number of apples in the basket: " << getCount() <<endl;

}
};

class Mangoes : public Fruits {

public:

void calculateMangoes() {

cout << "Enter the number of mangoes in the basket: ";

int numMangoes;

cin >> numMangoes;

addToBasket(numMangoes);

cout << "Number of mangoes in the basket: " << getCount() <<endl;

};

int main() {

Apples appleBasket;

Mangoes mangoBasket;

appleBasket.calculateApples();

mangoBasket.calculateMangoes();

return 0;

Output:

You might also like