You are on page 1of 24

1.

Write a program Illustrating class declarations, Definition, and Accessing class member

You can define classes using the keyword ‘class’ followed by the name of the class.

Here, inside the class, there are access-modifiers, data variables, and member functions. Now, understand them in
detail.
Access modifiers: These are the specifiers which provide or grant access for the members. They are of three types:
Private: Only members of the same class have access to private members.
Public: You can access the public members from within, as well as from outside of the class.
Protected: You can access the protected members from the same class members and members of the derived
class. It is also accessible from outside the class but with the help of the friend function.
Member-function: Member functions are standard functions declared inside a class. You can invoke it with the
help of the dot operator and object, which you will see later.
Note: It is necessary to put a semicolon at the end of the class.
Example:-

Accessing Class Member


Example:-
To access member variables and member functions of an object of a class, the . operator is used:

struct SomeStruct {
int a;
int b;
void foo() {}
};
SomeStruct var;
// Accessing member variable a in var.
std::cout << var.a << std::endl;
// Assigning member variable b in var.
var.b = 1;
// Calling a member function.
var.foo();

When accessing the members of a class via a pointer, the -> operator is commonly used. Alternatively, the instance can be
dereferenced and the . operator used, although this is less common:

struct SomeStruct {
int a;
int b;
void foo() {}
};

SomeStruct var;
SomeStruct *p = &var;
// Accessing member variable a in var via pointer.
std::cout << p->a << std::endl;
std::cout << (*p).a << std::endl;
// Assigning member variable b in var via pointer.
p->b = 1;
(*p).b = 1;
// Calling a member function via a pointer.
p->foo();
(*p).foo();
Q.2 Write a C++ Program to illustrate default constructor,parameterized constructor and copy
constructors

1. Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no
parameters.

It is also called a zero argument constructor.


#include <iostream>
using namespace std;
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl << "b: " << c.b;
return 1;
}

Output:-

a: 10
b: 20

2. Parameterized Constructors: It is possible to pass arguments to constructors. Typically, these arguments help initialize an
object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other
function. When you define the constructor’s body, use the parameters to initialize the object.
Program:- #include <iostream>
using namespace std;

class Point {
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}

int getX() { return x; }


int getY() { return y; }
};
int main()
{
// Constructor called
Point p1(10, 15);

// Access values assigned by constructor


cout << "p1.x = " << p1.getX()
<< ", p1.y = " << p1.getY();

return 0;
}

Output
p1.x = 10, p1.y = 15
3.Copy Constructor:
#include <iostream>
using namespace std;
class point {
private:
double x, y;

public:
// Non-default Constructor &
// default Constructor
point(double px, double py) { x = px, y = py; }
};

int main(void)
{

// Define an array of size


// 10 & of type point
// This line will cause error
point a[10];
// will compile without error
point b = point(5, 6);
}

3.WAP to find the largest of three numbers using inline function


Program:-
#include <iostream>
using namespace std;
inline int cmp(int x,int y,int z)
{
if(x>y&&x>z)
return(x);
else if(y>z)
return(y);
else
return(z);
}
int main()
{
int a,b,c;
cout<<"enter three numbers:"<<endl;
cin>>a>>b>>c;
cout<<cmp(a,b,c)<<" is larger"<<endl;
return 0;
}
Output:

4. Given that an EMPLOYEE class contains following members: data members: Employee number, Employee name, Basic, DA, IT, Net
Salary and print data members.

Program:
#include <windows.h>
#include <iostream>

using namespace std;

class employee
{
int emp_number;
char emp_name[20];
float emp_basic;
float emp_da;
float emp_it;
float emp_net_sal;
public:
void get_emp_details();
float find_net_salary(float basic, float da, float it);
void show_emp_details();
};
void employee :: get_emp_details()
{
cout<<"\nEnter employee number: ";
cin>>emp_number;
cout<<"\nEnter employee name: ";
cin>>emp_name;
cout<<"\nEnter employee basic: ";
cin>>emp_basic;
cout<<"\nEnter employee DA: ";
cin>>emp_da;
cout<<"\nEnter employee IT: ";
cin>>emp_it;
}
float employee :: find_net_salary(float basic, float da, float it)
{
return (basic+da)-it;
}
void employee :: show_emp_details()
{
cout<<"\n\n**** Details of Employee ****";
cout<<"\nEmployee Name : "<<emp_name;
cout<<"\nEmployee number : "<<emp_number;
cout<<"\nBasic salary : "<<emp_basic;
cout<<"\nEmployee DA : "<<emp_da;
cout<<"\nIncome Tax : "<<emp_it;
cout<<"\nNet Salary : "<<find_net_salary(emp_basic, emp_da, emp_it);
cout<<"\n-------------------------------\n\n";
}
int main()
{
employee emp;

emp.get_emp_details();
emp.show_emp_details();

return 0;
}

OUTPUT-
5. Write a C++ program to read data of N employee and computer net salary of each employee (DA = 52% of Basic and IT
= 30% of the gross salary).

Program-
#include<iostream.h>
#include<conio.h>

class Employee
{
char emp_name[30];
int emp_number;
float basic, da, it, gross_salary, net_salary;
public:
void read_emp_details(int count){
cout<<"\n\n*** Enter Employee "<<count<<" Details ***";
cout<<"\nEmployee Number: ";
cin>>emp_number;
cout<<"Employee Name: ";
cin>>emp_name;
cout<<"Basic Salary: ";
cin>>basic;
cout<<"\n---- Employee "<<count<<" Datails are saved ----\n\n";
}
float find_net_salary(){
da = basic * 0.52;
gross_salary = basic + da;
it = gross_salary * 0.30;
net_salary = (basic + da) - it;
return net_salary;
}
void display_emp_details(int count){
cout<<"\n\n*** Employee "<<count<<" Details ***\n";
cout<<"\nEmployee Number : "<<emp_number;
cout<<"\nEmployee Name : "<<emp_name;
cout<<"\nNet Salary: "<<net_salary;
cout<<"\n--------------------------\n";
}
};
int main(){
Employee emp[100];
int number_of_emp, count;
clrscr();
cout<<"\nPlease enter the number of Employees (Max. 100): ";
cin>>number_of_emp;
for(count=0; count< number_of_emp; count++){
emp[count].read_emp_details(count+1);
}
for(count=0; count < number_of_emp; count++){
emp[count].find_net_salary();
}
for(count=0; count < number_of_emp; count++){
emp[count].display_emp_details(count+1);
}
cout<<"\nPress any key to close!!!";
getch();
return 0;
}

OUTPUT-

6. Write a C++ Program to display Names, Roll No., and grades of 3 students who have appeared in the examination.
Declare the class of name, Roll No. and grade. Create an array of class objects. Read and display the contents of the array.
Program-
#include <iostream>
using namespace std;
class Student_Info{
int roll_number;
char student_name[50], grade[2];
public:
void read_data(int count){
cout<<"\n\n--------- Enter student "<<count+1<<" information ---------\n";
cout<<"Name of the Student (Max. 50 characters only): ";
cin>>student_name;
cout<<"Roll Number: ";
cin>>roll_number;
cout<<"Grade (O, A+, A, B+, B, C, D, F): ";
cin>>grade;
cout<<"\nStudent information with roll number "<<roll_number<<" has
saved!";
}
void display_data(int count){
cout<<"\n\n******** Student "<<count+1<<" Information ********";
cout<<"\nName of the Student: "<<student_name;
cout<<"\nRoll Number: "<<roll_number;
cout<<"\nGrade Secured: "<<grade;
cout<<"\n---------------------------------------\n";
}
};

int main(){
Student_Info stud[3];
int i;
for(i=0; i<3; i++)
stud[i].read_data(i);
cout<<"\n\n+++++++++++++++++++++++++++++++++++++++++++++++\n";
cout<<"The information of 3 students has been saved.";
cout<<"\n+++++++++++++++++++++++++++++++++++++++++++++++\n";
for(i=0; i< i++)
stud[i].display_data(i);
return 0;
}

Output-
7. Write a C++ program to use scope resolution operator. Display the various values of the same variables
declared at different scope levels.
Program-
#include <iostream>
using namespace std;

int my_variable = 10; // Global variable my_variable

int main()
{
int my_variable = 100; // Local variable my_variable

cout << "Value of global my_variable is " << ::my_variable << endl;
cout << "Value of local my_variable is " << my_variable << endl;

return 0;
}

OUTPUT-

8-Write a C++ program to allocate memory using new operator.

Program-
#include <iostream>
using namespace std;
class Rectangle{
int length, width;
public:
Rectangle(){
length = 2;
width = 5;
}
Rectangle(int l, int w){
length = l;
width = w;
}
void area(){
cout<< "Area of Rectangle is " << length * width << endl;
}
};
int main()
{
Rectangle *obj_1, *obj_2;
obj_1 = new Rectangle(); // Dynamic memory allocation
obj_2 = new Rectangle(3, 10); // Dynamic memory allocation

obj_1->area();
obj_2->area();

return 0;
}

OUTPUT-

9. WAP to Check Armstrong Number


Program-
#include <iostream>
using namespace std;

int main() {
int num, originalNum, remainder, result = 0;
cout << "Enter a three-digit integer: ";
cin >> num;
originalNum = num;

while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;
result += remainder * remainder * remainder;

// removing last digit from the orignal number


originalNum /= 10;
}

if (result == num)
cout << num << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";

return 0;
}

Output-

10-WAP to find HCF of two number

Program-
#include<iostream>
using namespace std;
int main()
{
int num1 = 36, num2 = 60, hcf = 1;

for(int i = 1; i <= num1 || i <= num2; i++)


{
if(num1 % i == 0 && num2 % i == 0)
hcf = i;
}
cout<<"HCF of "<<num1<<" and "<<num2<<" is "<<hcf;
return 0;
}

11.WAP to Check whether a number is a Strong Number or not


Program-

#include <iostream>
using namespace std;

int main()
{
int i, n, n1, s1 = 0, j;
long fact;
cout << "\n\n Check whether a number is Strong Number or not:\n";
cout << "----------------------------------------------------\n";
cout << " Input a number to check whether it is Strong number: ";
cin >> n;
n1 = n;
for (j = n; j > 0; j = j / 10) {
fact = 1;
for (i = 1; i <= j % 10; i++) {
fact = fact * i;
}
s1 = s1 + fact;
}
if (s1 == n1) {
cout << n1 << " is Strong number." << endl;
}
else {
cout << n1 << " is not a Strong number." << endl;
}
} OUTPUT

12. C++ Programs To Create Pyramid and Pattern


Program-
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}
OUTPUT

13. WAP to Find Prime number between 1 to 100


Program-
#include <iostream>
using namespace std;
int isPrimeNumber(int);
int main()
{
bool isPrime;
for(int n = 2; n < 100; n++) {
// isPrime will be true for prime numbers
isPrime = isPrimeNumber(n);
if(isPrime == true)
cout<<n<<" ";
}
return 0;
}

// Function that checks whether n is prime or not


int isPrimeNumber(int n) {
bool isPrime = true;
for(int i = 2; i <= n/2; i++) {
if (n%i == 0) {
isPrime = false;
break;
}
}
return isPrime;
}
OUTPUT-

14. WAP to perform matrix addition, matrix subtraction, matrix multiplication


Program-

#include<ioistream>
using namespace std;
int main()
{
//fill your code
int m, n;
cin >> m >> n;
int i, j;int mat1[m][n], mat2[m][n], mat3[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cin >> mat1[i][j];
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
cin >> mat2[i][j];
}

for(i = 0; i < m; i++)


{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cout << mat3[i][j] << ” “;
cout << endl;
}
return 0;
}

15. WAP to Call by Refrence and Call by Value


Program-
Call by value
#include<iostream.h>
#include<conio.h>

void swap(int a, int b)


{
int temp;
temp=a;
a=b;
b=temp;
}
void main()
{
int a=100, b=200;
clrscr();
swap(a, b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
}

2.CALL BY REFRENCE
#include<iostream.h>
#include<conio.h>

void swap(int *a, int *b)


{
int temp;
temp=*a;
*a=*b;
*b=temp;
}

void main()
{
int a=100, b=200;
clrscr();
swap(&a, &b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
getch();
}

16. Write a program in C++ to enter P, T, R and calculate Simple Interest.


Program-
#include<iostream>
using namespace std;

int main()
{
int p,r,t,i;
cout << "\n\n Calculate the Simple Interest :\n";
cout << " -----------------------------------\n";
cout<<" Input the Principle: ";
cin>>p;
cout<<" Input the Rate of Interest: ";
cin>>r;
cout<<" Input the Time: ";
cin>>t;
i=(p*r*t)/100;
cout<<" The Simple interest for the amount "<<p<<" for "<<t<<" years @ "<<r<<" % is: "<<i;
cout << endl;
return 0;
}

OUTPUT-

17. WAP to convert fahrenheit into celsius in C++


Program-
#include <iostream>
using namespace std;
int main()
{
float f, c;
cout << "Please enter temperature in fahrenheit\n";
cin >> f;
// converting fahreneheit to celsius

c = 5 * (f - 32) / 9;
cout << f <<" Fahrenheit is equal to =" << c <<" Centigrade";

return 0;
}

OUTPUT-

18. WAP to read distance kilometer and print meter centimeter millimeter c++
Program-
#include<iostream>
using namespace std;
int main()
{
float millimeter, centimeter, meter, kilometer;

cout << "\nPlease Enter the Length in Kilometers (km) = ";


cin >> kilometer;
meter = kilometer * 1000.0;
centimeter = kilometer * 100000.0;
millimeter = kilometer * 1000000.0;
cout << kilometer << " Kilometers = " << meter << " Meters" << endl;
cout << kilometer << " Kilometers = " << centimeter << " Centimeters" << endl;
cout << kilometer << " Kilometers = " << millimeter << " Millimeters";

return 0;
} OUTPUT
19. WAP to read a year and check their leap year condition by ternary operator

#include <iostream>

#include <conio.h>
using namespace std;
int main()
{
int year;//variable declaration
cout<<"Enter a year for check leap or not\n";
//ask input from the user
cin>>year;
//store the input in the year variable
if ((year%400==0)||((year%4==0)&&(year%100!=0))){
cout<<year<<" is a leap year ";
}//display leap year
else{
cout<<year<<" is not a leap year ";
}
getch();
return 0;
}

OUTPUT

20. WAP to read a number and print wheather it is odd or even in c++
Program-
#include <iostream>
using namespace std;

int main() {
int n;
cout << "Enter an integer: ";
cin >> n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";

return 0;
}
OUTPUT

21. WAP to Find the Area and Perimeter of a Rectangle


Program-
#include <iostream>
using namespace std;
int main()
{
int width, lngth, area, peri;
cout << "\n\n Find the Area and Perimeter of a Rectangle :\n";
cout << "-------------------------------------------------\n";
cout<<" Input the length of the rectangle : ";
cin>>lngth;
cout<<" Input the width of the rectangle : ";
cin>>width;
area=(lngth*width);
peri=2*(lngth+width);
cout<<" The area of the rectangle is : "<< area << endl;
cout<<" The perimeter of the rectangle is : "<< peri << endl;
cout << endl;
return 0;
}

OUTPUT

22. WAP to read four digit numvber and print its reverse c++
Program-
#include <iostream>
using namespace std;
int main() {

int n, reversed_number = 0, remainder;

cout << "Enter an integer: ";


cin >> n;

while(n != 0) {
remainder = n % 10;
reversed_number = reversed_number * 10 + remainder;
n /= 10;
}

cout << "Reversed Number = " << reversed_number;

return 0;
}

OUTPUT-

23.WAP to demonstrate template class


Program-
#include <iostream>
using namespace std;
template <typename T> T myMax(T x, T y)
{
return (x > y) ? x : y;
}
int main()
{
cout << myMax<int>(3, 7) << endl; // Call myMax for int
cout << myMax<double>(3.0, 7.0)
<< endl; // call myMax for double
cout << myMax<char>('g', 'e')
<< endl; // call myMax for char
return 0;
}
24. WAP to demonstrate template Funcation
Program-
#include <iostream>
using namespace std;

template <typename T>


T add(T num1, T num2) {
return (num1 + num2);
}

int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout << "2 + 3 = " << result1 << endl;

// calling with double parameters


result2 = add<double>(2.2, 3.3);
cout << "2.2 + 3.3 = " << result2 << endl;

return 0;
}
OUTPUT
Programming Lab in C++ MCA-207
LNCTU-2021-22
Sno List ofprograms Page
no
1 Write a program Illustrating class declarations, Definition, and Accessing class member 1-2

2 Write a C++ Program to illustrate default constructor,parameterized constructor 3-4


and copy constructors
3 WAP to find the largest of three numbers using inline function 4-5

4 Given that an EMPLOYEE class contains following members: data members: Employee number,
Employee name, Basic, DA, IT, Net Salary and print data members.

5 Write a C++ program to read data of N employee and computer net salary of each employee (DA =
52% of Basic and IT = 30% of the gross salary).

6 Write a C++ Program to display Names, Roll No., and grades of 3 students who have appeared in the
examination. Declare the class of name, Roll No. and grade. Create an array of class objects. Read and
display the contents of the array.

7 Write a C++ program to use scope resolution operator. Display the various values of the same
variables declared at different scope levels.
8 Write a C++ program to allocate memory using new operator.
9 WAP to Check Armstrong Number

10 WAP to find HCF of two number


11

WAP to Check whether a number is a Strong Number or not

12 C++ Programs To Create Pyramid and Pattern


13 WAP to Find Prime number between 1 to 100
14

WAP to perform matrix addition, matrix subtraction, matrix multiplication

15 WAP to Call by Refrence and Call by Value

16 Write a program in C++ to enter P, T, R and calculate Simple Interest.

17 WAP to convert fahrenheit into celsius in C++


18 WAP to read distance kilometer and print meter centimeter millimeter c++

19 WAP to read a year and check their leap year condition by ternary operator
20 WAP to read a number and print wheather it is odd or even in c++

21 WAP to Find the Area and Perimeter of a Rectangle

22 WAP to read four digit numvber and print its reverse c++
23 WAP to demonstrate template class

24 WAP to demonstrate template Funcations


25

You might also like