Lab Manual 07: CSC 2207 Programming Language 2 (EEE)
Lab Manual 07: CSC 2207 Programming Language 2 (EEE)
LAB MANUAL 07
CSC 2207 Programming Language 2 [EEE]
TITLE
PREREQUISITE
OBJECTIVE
THEORY
Encapsulation
#include<iostream>
using namespace std;
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;
}
};
public:
int area(){
return getHeight()*getWidth();
}
};
int main(void) {
Rectangle r;
r.setWidth(10);
r.setHeight(5);
return 0;
}
Inheritance Example
#include<iostream>
using namespace std;
void display(){
cout<<"ID: "<<id<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"CGPA: "<<cgpa<<endl;
}
};
int main(){
student s1;
s1.setId("19-23-4");
s1.setName("RR");
s1.setAge(25);
s1.setCgpa(3.5);
s1.display();
return 0;
}
#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;
}
};
float getCgpa(){
return cgpa;
}
};
int main(){
Student s(100,"Richard",3.5);
#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;
}
};
float getSalary(){
return salary;
}
};
float getDailyWages(){
return daily_wage;
}
};
int main(){
FullTimeEmployee fe(100,"Richard",5000);
PartTimeEmployee pe(200,"Philip",500);
return 0;
}
Polymorphism in C++
#include<iostream>
using namespace std;
class Base{
public:
void display(){
cout<<"I am the base class"<<endl;
}
};
};
int main(){
Derived d;
d.display();
Base d;
d.display();
#include<iostream>
using namespace std;
class Base{
public:
virtual void display(){
cout<<"Display of base"<<endl;
}
};
};
int main(){
Base *d = new Derived();
d->display();
return 0;
}
#include<iostream>
using namespace std;
class Base{
public:
virtual void display(){ //virtual function
};
public:
void display(){
};
int main(){
Base *p = new Base(); //
p->display();
p = new Derived();
p->display();
return 0;
}
✓ 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
};
public:
void draw(){
cout<<"Drawing the circle"<<endl;
}
};
};
};
int main(){
Shape *s = new Circle();
s->draw();
s = new Square();
s->draw();
s= new Triangle();
s->draw();
#include<iostream>
using namespace std;
class monster{
public:
virtual void attack()=0;
void moveforward(){
cout<<"Move forward."<<endl;
}
};
public:
void attack(){
cout<<"Fire Monster Attacking"<<endl;
}
};
public:
void attack(){
cout<<"water monster Attacking"<<endl;
}
};
int main(){
return 0;
}
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.