PL2 Lab6
PL2 Lab6
LAB MANUAL 06
CSC 2207 Programming Language 2 [EEE]
TITLE
PREREQUISITE
OBJECTIVE
THEORY
Structure in C
// 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++
#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 display(){
cout<<id<<" "<<name<<endl;
}
};
int main(){
student s1;
s1.set_data(10,"Rich");
s1.display();
}
Class in C++
#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(){
#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;
}
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;
}
• 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.
• 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;
}
#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;
}
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.