Oop LAB3
Oop LAB3
Bipasha Majumder
Lecturer, Dept of CSE
Dhaka International University
1
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
public:
int id; // Member of Class
double gpa; // Member of class
void display() // Function of the class
{
cout<<id<<" "<<gpa<<endl;
}
student(int x,double y) // Constructor_Special Function :
{ // To set value into variable id and GPA
id=x;
gpa=y;
}
}; 2
int main()
{
student Alim(101,3.50);
Alim.display();
student Karim(102, 3.60);
Karim.display();
getch();
}
3
class student
{
public:
int id; // Member of Class
double gpa; // Member of class
void display() // Function of the class
{
cout<<id<<" "<<gpa<<endl;
}
student(int x,double y)
{
id=x;
gpa=y;
}
};
student() // Default Constructor_Special Function
{
cout<< “ Defaulf constructor”;
} 4
int main()
{
//No value passed
student Rahim; //It’ll call devalufault constructor
student Alim(101,3.50);
Alim.display();
5
#include <iostream>
using namespace std;
class Test
{
public:
// User-Defined Constructor
Test() { cout << "\n Constructor executed";
}
// User-Defined Destructor
~Test() { cout << "\nDestructor executed";
}
};
main()
{ Output -
Test t; Constructor executed
Destructor executed
return 0;
6
}
// C++ program to demonstrate
// Encapsulation
#include <iostream>
using namespace std;
class Encapsulation
{
private:
// Data hidden from outside world
int x;
public:
// Function to set value of Output-
// variable x 5
void set(int a) { x = a; }
// Function to return value of
// variable x
int get() { return x; }
}; 7
int main()
{
Encapsulation obj;
obj.set(5);
cout << obj.get();
return 0;
}
8
Inheritance
The capability of a class to derive properties and characteristics from
another class is called Inheritance. Inheritance is one of the most
important features of Object-Oriented Programming.
Inheritance is a feature or a process in which, new classes are created from
the existing classes. The new class created is called “derived class” or “child
class” and the existing class is known as the “base class” or “parent class”.
The derived class now is said to be inherited from the base class.
Syntax:
class <derived_class_name> : <access-specifier>
<base_class_name>
{
//body
}
9
#include <iostream>
using namespace std;
class Person {
int id;
char name[100];
public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}
void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name
<<endl;
}
}; 10
class Student : private Person {
char course[50];
int fee;
public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}
void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee <<
endl;
}
};
11
int main()
{
Student s;
s.set_s();
s.display_s();
return 0; Output:
} Enter the Id: 101
Enter the Name: RONI
Enter the Course Name:CSE
Enter the Course Fee:1000
Id: 101
Name: RONI
Course: CSE
Fee: 1000
12
Lab Report
A. Write a program to implement a class called Circle that has
private member variables for radius. Include member functions
to calculate the circle's area and circumference.
B. Write a program to create a class called Person that has
private member variables for name, age and country.
Implement member functions to set and get the values of these
variables.
C. Write a program that defines a shape class with a
constructor that gives value to width and height. The define
two sub-classes triangle and rectangle, that calculate the area
of the shape area (). In the main, define two variables a triangle
and a rectangle and then call the area() function in this two
varibles.
13
D. Write a program to declare class BOX private height,
width, depth public declare 1 default constructor, 1
parameterized constructor and show() to display the values.
Write main() function to declare an object to invoke all the
constructor and show function for each object.
E. Write a program to print the area and perimeter of a
triangle having sides of 3, 4 and 5 units by creating a class
named 'Triangle' with the constructor having the three sides
as its parameters.
F. Write a program which will calculate the area of circle and
rectangle using a base class shape and derived elasses cirele
and rectangle.
14