Professional Documents
Culture Documents
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:-
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.
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;
}
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)
{
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>
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 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-
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-
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;
if (result == num)
cout << num << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";
return 0;
}
Output-
Program-
#include<iostream>
using namespace std;
int main()
{
int num1 = 36, num2 = 60, hcf = 1;
#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
#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];
}
2.CALL BY REFRENCE
#include<iostream.h>
#include<conio.h>
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();
}
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-
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;
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
OUTPUT
22. WAP to read four digit numvber and print its reverse c++
Program-
#include <iostream>
using namespace std;
int main() {
while(n != 0) {
remainder = n % 10;
reversed_number = reversed_number * 10 + remainder;
n /= 10;
}
return 0;
}
OUTPUT-
int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout << "2 + 3 = " << result1 << 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
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
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++
22 WAP to read four digit numvber and print its reverse c++
23 WAP to demonstrate template class