0% found this document useful (0 votes)
40 views

Lab Manual 07: CSC 2207 Programming Language 2 (EEE)

The electro-optic sensor contains an infrared LED and a light receiver. Light from the LED is directed into a prism which forms the tip of the sensor. With no liquid present, light from the LED is reflected within the prism to the receiver. When rising liquid immerses the prism, the light is refracted out into the liquid, leaving little or no light to reach the receiver. Sensing this change, the receiver actuates electronic switching within the unit to operate an external alarm or control circui

Uploaded by

Sayed Mashal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Lab Manual 07: CSC 2207 Programming Language 2 (EEE)

The electro-optic sensor contains an infrared LED and a light receiver. Light from the LED is directed into a prism which forms the tip of the sensor. With no liquid present, light from the LED is reflected within the prism to the receiver. When rising liquid immerses the prism, the light is refracted out into the liquid, leaving little or no light to reach the receiver. Sensing this change, the receiver actuates electronic switching within the unit to operate an external alarm or control circui

Uploaded by

Sayed Mashal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

American International University Bangladesh (AIUB)

Faculty of science & Technology


Department of Computer Science

LAB MANUAL 07
CSC 2207 Programming Language 2 [EEE]
TITLE

Encapsulation, Inheritance, Polymorphism and abstraction C++

PREREQUISITE

• To know about class and object


• To know about constructor and destructor

OBJECTIVE

• To get a basic idea about inheritance in OOP


• To know how to implement inheritance in C++
• To understand about polymorphism in C++

THEORY

Encapsulation

• Encapsulation is defined as the wrapping up of data under a single unit. It is the


mechanism that binds together code (functions) and the data it manipulates.
• Example: C++ class, C++ structure

#include<iostream>
using namespace std;

class Student{ // encapsulation


//fields or data members
public:
int id;
string name; //member functions or methods
double cgpa;

void display(){
cout<<"ID: "<<id<<" Name: "<<name<<endl;
}
};
int main(){

Student s1;
s1.id =100;
s1.name = "Richard";
s1.display();
}

Array of Objects

#include<iostream>
using namespace std;

class Student{

public:
int id;
string name;
double cgpa;

void display(){
cout<<"ID: "<<id<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Cgpa: "<<cgpa<<endl;
}
};

int main(){

Student s[2];

for(int i=0;i<2;i++){
cout<<"Student # "<<i<<endl;
cin>>s[i].id>>s[i].name>>s[i].cgpa;
}
cout<<endl;

for(int i=0;i<2;i++){
cout<<"Student # "<<i<<endl;
s[i].display();
}

}
Inheritance in C++
✓ Inheritance is one of the key features of Object-oriented programming in C++.
✓ It allows us to create a new class (derived class) from an existing class (base class).
✓ The derived class inherits the features from the base class and can have additional
features of its own.

#include <iostream>
using namespace std;

class Shape {

protected:
int width;
int height;

public:
//setter
void setWidth(int w){
width=w;
}
void setHeight(int h){
height = h;
}
//getter

int getWidth(){
return width;

int getHeight(){
return height;

}
};

class Rectangle: public Shape{

public:

int area(){

return getHeight()*getWidth();
}

};

int main(void) {
Rectangle r;
r.setWidth(10);
r.setHeight(5);

cout<<"Area of Rectangle: "<<r.area()<<endl;

return 0;
}

Inheritance Example

#include<iostream>
using namespace std;

class person{ //parent class/base class


public:
string name;
int age;

void setName(string n){


name=n;
}

void setAge(int a){


age =a;
}
};

class student: public person{ // using inheritance


//properties
public:
string id;
float cgpa;

void setId(string i){


id=i;
}
void setCgpa(float c){
cgpa = c;
}

void display(){
cout<<"ID: "<<id<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"CGPA: "<<cgpa<<endl;
}
};

class employee:public person{


string id;
float salary;
};

int main(){

student s1;
s1.setId("19-23-4");
s1.setName("RR");
s1.setAge(25);
s1.setCgpa(3.5);
s1.display();

return 0;
}

Constructor Inheritance in C++

#include<iostream>
using namespace std;

class Person{
private:
int id;
string name;
public:
Person(int iid, string n){
cout<<"person constructor"<<endl;
id=iid;
name=n;
}
int getId(){
return id;
}

string getName(){
return name;
}
};

class Student:public Person{


private:
float cgpa;
public:
Student(int i, string n, float c):Person(i,n){
cout<<"student constructor"<<endl;
cgpa=c;
}

float getCgpa(){
return cgpa;
}
};

int main(){

Student s(100,"Richard",3.5);

cout<<s.getId()<<" "<<s.getName()<<" "<<s.getCgpa()<<endl;

Constructor Inheritance in C++

#include<iostream>
using namespace std;

class Employee{
private:
int empid;
string empname;
public:
Employee(int id, string name){
empid=id;
empname = name;
}
int getEmpId(){
return empid;
}

string getName(){

return empname;
}
};

class FullTimeEmployee:public Employee{


private:
float salary;
public:
FullTimeEmployee(int id, string name, float s):Employee(id,name){
salary =s;
}

float getSalary(){
return salary;
}
};

class PartTimeEmployee:public Employee{


private:
float daily_wage;
public:
PartTimeEmployee(int id, string name, float dw):Employee(id,name){
daily_wage =dw;
}

float getDailyWages(){
return daily_wage;
}
};

int main(){

FullTimeEmployee fe(100,"Richard",5000);

cout<<fe.getEmpId()<<" "<<fe.getName()<<" "<<fe.getSalary()<<endl;

PartTimeEmployee pe(200,"Philip",500);

cout<<pe.getEmpId()<<" "<<pe.getName()<<" "<<pe.getDailyWages()<<endl;

return 0;
}

Polymorphism in C++

#include<iostream>
using namespace std;

class Base{
public:
void display(){
cout<<"I am the base class"<<endl;
}
};

class Derived:public Base{


public:
void display(){
cout<<"I am the derived class"<<endl;
}

};

int main(){

Derived d;
d.display();

Base d;
d.display();

Base Class Pointer Derived Class Object

#include<iostream>
using namespace std;

//Base Class Pointer Derived Class Object

class Base{
public:
virtual void display(){
cout<<"Display of base"<<endl;
}
};

class Derived:public Base{


public:
void display(){
cout<<"Display of derived "<<endl;
}

};

int main(){
Base *d = new Derived();
d->display();

return 0;
}

Run Time Polymorphism

✓ The main use of virtual function is to achieve Runtime Polymorphism.


✓ Runtime polymorphism can be achieved only through a pointer (or reference) of base
class type. Also, a base class pointer can point to the objects of base class as well as to
the objects of derived class.

#include<iostream>
using namespace std;

class Base{
public:
virtual void display(){ //virtual function

cout<<"display in Base class"<<endl;


}

};

class Derived: public Base{

public:
void display(){

cout<<"display in Derived class"<<endl;


}

};

int main(){
Base *p = new Base(); //

p->display();

p = new Derived();

p->display();

return 0;
}

Pure Virtual function, abstract class and interface

✓ A pure virtual function or pure virtual method is a virtual function that is required to be
implemented by a derived class if the derived class is not abstract.
✓ Classes containing at least one pure virtual method are termed "abstract" class and they
cannot be instantiated directly.
✓ 100% abstract class (all methods are pure virtual) is called interface in other OOP
language.

#include<iostream>
using namespace std;

class Shape{ // Shape is interface because all of its methods are pure virtual
public:
virtual void draw()=0; // abstract method
};

class Circle:public Shape{

public:
void draw(){
cout<<"Drawing the circle"<<endl;
}

};

class Square:public Shape{


public:
void draw(){
cout<<"Drawing the Square"<<endl;
}

};

class Triangle:public Shape{


public:
void draw(){
cout<<"Drawing the Triangle"<<endl;
}

};

int main(){
Shape *s = new Circle();
s->draw();

s = new Square();
s->draw();

s= new Triangle();

s->draw();

Example of Abstract class

#include<iostream>
using namespace std;

class monster{

public:
virtual void attack()=0;

void moveforward(){
cout<<"Move forward."<<endl;
}

};

class firemonster:public monster{

public:

void attack(){
cout<<"Fire Monster Attacking"<<endl;
}
};

class watermonster:public monster{

public:

void attack(){
cout<<"water monster Attacking"<<endl;
}
};
int main(){

cout<<"Fire monster tern: "<<endl;


monster *m = new firemonster();
m->attack();
m->moveforward();

cout<<"water monster tern: "<<endl;


m = new watermonster();
m->attack();
m->moveforward();

return 0;
}

Compile time Polymorphism

Friend function and friend class

LAB WORK/ ASSIGNMENT

1. Write a class for employee with private empId and empname; constructor and getter
methods. Derived classes Full time employee with private salary and constructor and
getter methods. Part time employee with private daily wage and constructor and getter
methods. In the main function, create part time employee and full time employee and call
getter to display.
2. Create a simple “shape” hierarchy: a base class called Shape and derived classes called
Circle, Square, and Triangle. In the base class, make a virtual function called draw( ), and
override this in the derived classes. Make a pointer of Shape and objects of derived class
and call draw( ) through the base-class pointers, to verify the behavior of the virtual
function.
3. Re write the example 2 using pure virtual functions.
4. Create a simple “Car” hierarchy: a base class called Car and derived classes called
Toyota, Honda, and Mazda. In the base class, make a virtual function called start( ), and
override this in the derived classes. Make a pointer of Car and objects of derived class
and call start( ) through the base-class pointers, to verify the behavior of the virtual
function.

You might also like