Object Oriented Programming Practical 4

Question 1
Write a program to define a class student having data members name and roll no. Accept and display data for one object. Define the member function inside the class.
student.cpp
C
				#include <iostream.h> 
#include <conio.h>   

class Student 
{
	private:
		char name[30];
		int rollNo;

	public:
		void acceptData() 
		{
			cout << "Enter student name: ";
			cin >> name; 
			cout << "Enter roll number: ";
			cin >> rollNo;
		}


		void displayData() 
		{
			cout << "\n--- Student Details ---" << endl;
			cout << "Name: " << name << endl;
			cout << "Roll Number: " << rollNo << endl;
		}
};

int main() 
{
    Student student1;
    student1.acceptData();
    student1.displayData();
	getch();
	return 0; 
}
			
Output
Question 2
Write a program to calculate square of number in which define class and define member function inside the class as per requirement.
square.cpp
C
				#include <iostream> 
#include <conio.h>   

class SquareCalculator 
{
	public: 
		int num;
		
		void accept()
		{
			cout << "Enter a number: ";
			cin >> num;
		}
		void calculateSquare() 
		{
			int sq=num * num; 
			cout<<"Square of number is: "<<sq<<endl;
		}
};

int main() 
{
    SquareCalculator calculator;
	calculator.accept();
	calculator.calculateSquare();
	getch();
    return 0; 
}
			
Output
Question 3
Define a class Room with data members length, breadth and height. Member function calculate_area () and calculate_volume(). Calculate the area and volume of room. Define the member function inside the class.
room.cpp
C
				#include<iostream.h>
#include<stdio.h>
#include<conio.h>

class room
{
   public:
	int length,breadth,height;

	void accept()
   {
		cout<<"\nEnter length of a room: ";
		cin>>length;
		cout<<"\nEnter breadth of a room: ";
		cin>>breadth;
		cout<<"\nEnter height of a room: ";
		cin>>height;
	}

	void calculate_area()
	{
		int area;
		area=length*breadth;
		cout<<"Area of room is: "<<area<<" square feets"<<endl;
	}
	
	void calculate_volume()
	{
		int volume;
		volume=length*breadth*height;
		cout<<"Volume of room is: "<<volume<<" cubic feets"<<endl;
	}
};

void main()
{
	clrscr();
	room r;
	r.accept();
	r.calculate_area();
	r.calculate_volume();
	getch();
}

			
Output
Question 4
Define a class mean in which assign two numbers in assign member function (i.e. assign(4,8)) passed value from main function and define assign member function inside the class and display the mean of two number on output screen
mean.cpp
C
				#include<iostream.h>
#include<stdio.h>
#include<conio.h>

class mean
{
   public:
	int num1,num2;

	void assign(int x,int y)
   {
		num1=x;
		num2=y;
	}

	void display()
	{
		float m;
		m=(num1+num2)/2.0;
		cout<<"Mean is: "<<m<<endl;
	}
};

void main()
{
	clrscr();
	int a,b;
	mean m;
	cout<<"Enter number 1: ";
	cin>>a;
	cout<<"Enter number 2: ";
	cin>>b;
	m.assign(a,b);
	m.display();
	getch();
}

			
Output

Watch full practical here

Leave a Comment

Your email address will not be published. Required fields are marked *