0% found this document useful (0 votes)
20 views11 pages

Lab Report 7 Muhammad ABdullah Zafar Ghauri ME - 14 405642

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

Lab Report 7 Muhammad ABdullah Zafar Ghauri ME - 14 405642

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Fundamentals of Programming Lab

Lab Report 07

1st SEMESTER

Submitted to: Engr. Ali Hassan


Session: ME-14 Section: C Group: 2
SUBMITTED BY
Sr. Lab
No. Names CMSID Report Lab work Lab Tasks Total
(Theory)
1 Muhammad Abdullah 405642
Zafar Ghauri

School of Mechanical and Manufacturing Engineering


Objectives:

After this lab ,we as students shall be greatly able to

• Understand how to make classes


• Understand about the types of constructors (Default and Parameterized Functions)
• How to declare the default constructors in program
• How to declare the parameterized constructors in program
• Using .h file in .cpp file as calling of class in program

Theory:

1. Classes in C++ Program:


A class is a usder-defined data tuype in C++ program that can
be made in a program by a programmer . The class in a program works as a object
constructor , or for creating objects.
2. Object Constructor:
Object Constructor in a program turns the provided input into an
object. It usually returns the value, for example if null entry is provided to it, it creates
and returns an empty object.
3. Constructors:
A Constructor is a special method called automatically when an object of a
class is created.
To create a constructor, use the same name as the class, followed by parentheses ():
Generally speaking, there are two types of constructors in a C++ Program:
▪ Default Constructor
▪ Parameterized Constructor
• Default Constructor:
Default constructor is a type of constructor in C++ program that
has either no parameters, or if it has some parameters, they have default values. If no
user-defined constructor exists for a very class say A, then the default constructor can be
said as
Class A
{
public:
A(); //Default Constructor with no Assigned Value
A(int=0); //Default Constructor with one assigned value, 0 to integer type
};
• Parameterized Constructor:
Parameterized constructor is a type of constructor in
which accepts a specific number of parameters
Consider the following program
#include<iostream>
using namespace std;
class point
{
Private:
double xCoord , yCoord;
public:
point(); //Default Constructor
point(double x, double y); //Parameterized Constructor
double operator – (const point & rhs)const;
bool operator == (const point & rhs) const;
};
In the following program, both the Default and Parameterized Constructors have been
declared.
LAB ASSIGNMENT
General Self-Information:

#include<iostream>
using namespace std;
int main()
{
cout << "\n My Name is Abdullah Zafar";
cout << "\n I Study in ME-14 (C) ";
cout << "\n My CMS ID is 405642";
return 0;
}
Output:

Q.1 Write a program in C++ comprising of class, user-defined functions with also
including default and parameterized functions.
• Program as Input:

#include<iostream>
using namespace std;
class area
{
private:
double base, height;

public:
area(); //Default Constructor
area(double _base, double _height); //Parameterized Function

double calculateArea()
{
return base*height;
}
};

Here header file has been declared as Area.h. It is to be called later in .cpp file.

#include<iostream>
#include<cmath>
#include"area.h"
using namespace std;

area::area()
{
cout << "\nInitializing the default values,i.e. 0,0";
base = 0.0;
height = 0.0;
}
area::area(double _base, double _height)
{
cout << "\nInitalizing with the values";
base = _base;
height = _height;

}
int main()
{
double result = 0;

area obj;
area obj2(10.0, 20.0);
result= obj2.calculateArea();
cout << "\nThe Result as after calcualtion of area is=" << result;
return 0;
}

Output:
LAB TASKS:

Q.1 Make a user-defined function displaying the messages of condolences .

Program:

#include<iostream>
using namespace std;
class arshad_shareef
{

private:
int a, b;
public:
int arshad()
{
cout << "I pay my respect to him and condolences";
cout << "\n to him family";
return 0;
}

arshad_shareef()
{
cout << "I am declared";
arshad();
}
arshad_shareef(int xy, int yz)
{
cout << "I am declared parameterized";
cout << "\n with values" << xy << " " << yz;
}
};
int main()
{
arshad_shareef obj;
obj.arshad();
}
Output:

Q.2 Write a program to convert lower case alphabets to upper-case alphabets .

Program as Input:

#include<iostream>
#include<cmath>
using namespace std;

int country();
float product(float a, float b);
char toupper(char a);

int main()
{
cout << toupper('s');
}
char toupper(char a)
{
return a - 32;

}
float product(float a, float b)
{
return a * b;
}
int country()
{
cout << "\n They lost in a fit of lust of power and proudness and
overconfidence!!!";
return 0;
}

Output:

Q.3 Write a random class program

Input:

#include<iostream>
#include<cmath>
using namespace std;

class programming
{
private:
int a;
int b;
public:
programming(); //Default Constructor
//programming(int x, int y); //Parameterized Constructor
bool user(int x, int y)
{
return x >= y;
}
int point(int x, int y)
{
a = x;
b = y;
}
int point2(int x, int y)
{
return sqrt(pow(x - a, 2) + pow(x - b, 2));
}
};
int main()
{

Output:

Declartion in .h file. Its implementation in .cpp file is as follows

#include<iostream>
#include "programming.h" //Calling the member class from .h file
using namespace std;

programming::programming() //Default Construction


{
cout << "salamalaikum";
}

programming::programming(int x, int y)
{
cout << "Bonjour! Hello Wie geht's?";
a = x;
b = y;
}

int main()
{

programming ab(7, 9), ab;


cout << ab.point2(7, 9);

Q. Write another class

#include<iostream>
using namespace std;

class pakistan
{
private:
int a, b;
public:
int quaid()
{
cout << "History taeches man great everlasting deep lessons that should
never be forgotten!" << endl;
a = 0;
b = 0;
cout << endl << a << " " << b;
return 0;
}
pakistan() //default constructor
{
cout << "Bonjour Wie Gehts? Es Geht's Danke! Es Geht wie allen?";
a = 0;
b = 0;

}
pakistan(int x, int y)
{
cout << "Es Geht Wie Allen?" << x * y << " times";

char toupper(char a)
{
return a - 32;
}

int pow(int a, int b)


{
switch (b)
{
case 0:
return 1;
break;
case 1:
return a;
break;
case 2:
return a * a;
break;
case 3:
return a * a * a;
break;
default:
cout << "Invalid input";
}
}
};
int main()
{
pakistan obj;
obj.quaid();
}

Output:

You might also like