Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

American International University Bangladesh (AIUB)

Faculty of science & Technology


Department of Computer Science

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

A brief Introduction to OOP and C++ class

PREREQUISITE

• To know about variables


• To be able to use cin and cout
• To know about arrays
1. To know about functions
2. To know about loop control structure
3. To know about decision control structure

OBJECTIVE

• To get a basic idea about OOP


• To gain experience and understand about class and object
• To know about different constructors and destructor
• To know about access modifier, property function, this pointer and pointer to object.

THEORY

Structure in C

• Structure in C doesn’t have any member functions


• By default all member variables are public.

// Structure in C
#include<stdio.h>
struct student{
//By default Data members are public.
int id;
char name[50];
// There is no member functions
};
int main(){

struct student s;
s.id=100;
strcpy(s.name,"Rich");

printf("%d\n",s.id);
printf("%s\n",s.name);

return 0;
}

Structure in C++

• There are member functions in C++ structure.


• By default all member variables and member functions are public.

#include<iostream>
using namespace std;

struct student{
// there are member functions in C++ struct
// By default all member variables
//and member functions are public
int id;
string name;

void set_data(int idd, string n){


id= idd;
name=n;
}

void display(){
cout<<id<<" "<<name<<endl;
}
};

int main(){

student s1;

s1.set_data(10,"Rich");
s1.display();
}

Object Oriented Programming

Object-oriented programming (OOP) is a programming paradigm based on the concept of


"objects", which can contain data, in the form of fields (often known as attributes or properties),
and code, in the form of procedures (often known as methods). There are four Pillars of Object
Oriented Programming:

• Abstraction: Abstraction of Data or Hiding of Information is called Abstraction!


• Encapsulation: Binding of Data and Functions together and keep both safe from outside
interference and misuse is called Encapsulation.
• Inheritance: Inheritance enables new objects to take on the properties of existing
objects.
• Polymorphism: Polymorphism is derived from 2 Greek words: poly and morphs. The
word "poly" means many and "morphs" means forms. So polymorphism means "many
forms".

Class in C++

• C++ is an object-oriented programming language. Everything in C++ is associated with


classes and objects, along with its attributes and methods.
• A class in C++ is the building block, it is a user-defined data type, which holds its own
data members and member functions, which can be accessed and used by creating an
instance of that class.
• By default the member variables and member functions are private in C++ Class. That
means, you can’t access those from outside the class. If you want to make those public,
need to use public keyword.

Example Rectangle class

#include<iostream>
using namespace std;

class Rectangle{
public:
int length; //member variables of class
int breadth;
int area(){//member function of class
return length*breadth;
}

int perimeter(){
return 2*(length+breadth);
}
};
int main(){

//create an object of Rectangle r1


Rectangle r1;
r1.length = 2;
r1.breadth =3;
cout<<"Area "<<r1.area()<<endl;
cout<<"Perimeter "<<r1.perimeter()<<endl;
return 0;
}

Example Box class

#include<iostream>
using namespace std;

class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(){
return length*breadth*height;
}
};

int main() {
// Declare Box1 of type Box
Box box1;
// box 1 specification
box1.height = 5.0;
box1.length = 6.0;
box1.breadth = 7.0;
double volume = box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
return 0;
}

A member function of a class


• A member function of a class is a function that has its definition or its prototype within
the class definition like any other variable.
• You can define the same function outside the class using the scope resolution operator (::)

class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void); // function prototype
};

double Box::getVolume(void) {
return length * breadth * height;
}

C++ Class Access Modifiers

• The access restriction to the class members is specified by the labeled public, private,
and protected sections within the class body.
• A public member is accessible from anywhere outside the class but within a program.
• A private member variable or function cannot be accessed, or even viewed from outside
the class. Only the class and friend functions can access private members.

Private data members and property functions

• A private member variable or function cannot be accessed, or even viewed from outside
the class. Only the class and friend functions can access private members.
• Property functions are getXXX and setXXX
o getXXX - Accessors
o setXXX - Mutators

#include<iostream>
using namespace std;
class Rectangle{
private:
int length; // private data members
int breadth;
public:
int getLength(){
return length;
}
void setLength(int l){
length = l;
}
int getBreadth(){
return breadth;
}
void setBreadth(int b){
breadth = b;
}
int area(){
return length*breadth;
}
int perimeter(){
return 2*(length+breadth);
}
};
int main(){
//create an object of Rectangle r1
Rectangle r1;
r1.setLength(5);
r1.setBreadth(4);
cout<<"Area "<<r1.area()<<endl;
cout<<"Perimeter "<<r1.perimeter()<<endl;
return 0;
}

this pointer

Every object in C++ has access to its own address through an important pointer called this
pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a
member function, this may be used to refer to the invoking object.

#include<iostream>
using namespace std;

class Rectangle{
private:
int length;
int bredth;
public:
void setLength(int length){
this->length = length;
}
void setBredth(int bredth){
this->bredth = bredth;
}
int getLength(){
return length;
}
int getBredth(){
return bredth;
}
int area(){
return length*bredth;
}
};
int main(){
//Object of Rectangle
Rectangle r1;
r1.setLength(10);
r1.setBredth(20);
cout<<"area of Rectangle: "<<r1.area()<<endl;
}

Pointer to an object

#include<iostream>
using namespace std;

class Rectangle{
public:
int length;
int bredth;
int area(){
return length*bredth;
}
};

int main(){
//Object of Rectangle
Rectangle r1;
r1.length=10;
r1.bredth = 20;
cout<<"area of Rectangle: "<<r1.area()<<endl;
//pointer to an object
Rectangle *p;
p=&r1;
p->length = 20;
cout<<"Area of Rectangle: "<<p->area()<<endl;
return 0;
}

C++ Class Constructor and Destructor


• A class constructor is a special member function of a class that is executed whenever we
create new objects of that class.
• A constructor will have exact same name as the class and it does not have any return type
at all, not even void.
• Constructors can be very useful for setting initial values for certain member variables.

#include<iostream>
using namespace std;

class Rectangle{
private:
int length;
int breadth;
public:
//default constructor
Rectangle(){
length=0;
breadth =0;
cout<<"Default constructor"<<endl;
}
// destructor
~Rectangle(){
cout<<"destructor"<<endl;
}

//parameterized constructor
Rectangle(int l, int b){
length=l;
breadth =b;
cout<<"Parameterized constructor"<<endl;
}
// copy constructor
Rectangle(Rectangle &rect){
length = rect.length;
breadth = rect.breadth;
}

int area(){
return length*breadth;
}
};

int main(){
//calling default constreuctor
Rectangle r;
cout<<"Area "<<r.area()<<endl;
// calling parameterized constructor
Rectangle r1(10,10);
cout<<"Area "<<r1.area()<<endl;
//calling copy constructor
Rectangle r2(r1);
cout<<"Area "<<r2.area()<<endl;
return 0;
}

LAB WORK/ ASSIGNMENT

1. Write a program that defines a class called Employee and fields/ member variables are
“empId” type interget, ”empName” of type string and ”salary” type of double. The class
also has a display function to prints the employee information.
2. Write a program that defines a class named Point with member variables to x and y
coordinates as double. The class also has a display function to prints all information.
3. Using number 2, in main function, declare array of point objects and take inputs the
coordinate from the users and print them.
4. Write a program that defines a class called Student and private fields are “studentId” type
interget, ”studentName” of type string and ”cgpa” type of double. The class also has
setter and getter methods and display function to prints the student information. Use this
keyword to set and get the values of member variables.
5. Write a program that defines a class called Student and private fields are “studentId” type
interget, ”studentName” of type string and ”cgpa” type of double. The class also has all
type of constructors (default, parameterized and copy) and display function to prints
the information.

You might also like