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

Term Work

Course : BSC.IT
Name : Ansh Gujral
Roll No: 09
UNI Roll No: 2200709
Subject: C++ lab

Submitted to:
Mrs. Vandana Rawat Maam
Name: Ansh Gujral------Roll no.09------Uni.Roll No:2200709------BSc.IT. ------Sem2------

Q1.Write a program in c++ for sum of two numbers and cube using inline
function.
OBJECTIVE: To learn the concept of Inline function.
#include<iostream>
using namespace std;
class ansh // class name
{
public: //access specifier
inline void cube(int a) //inline function
{
int cube =a*a*a; // Ansh Gujral
cout<< " cube is: "<<cube;
}
inline void sum(int a,int b)
{
int add=a+b;
cout<<"addition: "<<add;
}
};
int main() //main function
{
int a;
int b;
cout<<"enter the numbers:";
cin>>a>>b;
ansh s; //object calling
s.sum(a,b);
s.cube(a);}

C++ LAB
Name: Ansh Gujral------Roll no.09------Uni.Roll No:2200709------BSc.IT. ------Sem2------

Output:
enter the numbers:5 8
addition: 13 cube is: 125
Press any key to continue . . .

C++ LAB
Name: Ansh Gujral------Roll no.09------Uni.Roll No:2200709------BSc.IT. ------Sem2------

Q2. Write a program in c++ to access a global variable using scope


resolution operator.
OBJECTIVE: To learn the concept of scope resolution operator with
global variable,
#include<iostream>
using namespace std;
int num=151;
int main()
{
int num=62;
cout<<"the global variable is:"<<::num<<endl;//151
cout<<"the local variable is: "<<num;//62

C++ LAB
Name: Ansh Gujral------Roll no.09------Uni.Roll No:2200709------BSc.IT. ------Sem2------

Output:
the global variable is:151
the local variable is: 62
Press any key to continue . . .

C++ LAB
Name: Ansh Gujral------Roll no.09------Uni.Roll No:2200709------BSc.IT. ------Sem2------

Q3.Write a program in c++ to get detail of students and print the details by
creating object.
OBJECTIVE: To learn the concept of c++.
#include<iostream>
using namespace std;
class student //class name
{
public: //access specifier
char stu_name[12];
int roll_no;
char course_name[20];
long long ph_no;
void get_details() //member function
{
cout<<"enter the student name:";
cin>>stu_name;
cout<<"enter the roll no of students:";
cin>>roll_no;
cout <<"enter the course name:";
cin>>course_name;
cout<<"enter the student's phone number:";
cin>>ph_no;
}
void put_details() //member function
{
cout<<" The student details are:- \n";
cout<<"\n *************";
cout<<" \nThe student name is :"<<stu_name;

C++ LAB
Name: Ansh Gujral------Roll no.09------Uni.Roll No:2200709------BSc.IT. ------Sem2------

cout<<" \nThe student roll no is :"<<roll_no;


cout<<" \nThe student's course is :"<<course_name;
cout<<"\n The student's phone number is :"<<ph_no;
}
};
int main() //main function
{
student s;
s.get_details(); // object calling
s.put_details(); // object calling
}

C++ LAB
Name: Ansh Gujral------Roll no.09------Uni.Roll No:2200709------BSc.IT. ------Sem2------

Output:
The student details are:-

*************
The student name is :ansh
The student roll no is :2200709
The student's course is :BscIT
The student's phone number is :8979754244
Press any key to continue . . .

C++ LAB
Name: Ansh Gujral------Roll no.09------Uni.Roll No:2200709------BSc.IT. ------Sem2------

Q4. Write a program in c++ to access a static variable using scope


resolution operator.
OBJECTIVE: To learn the concept of scope resolution with static

#include <iostream>  
using namespace std;   
class Parent  
{  
static int n1;  
public:  
static int n2;  
// The class member can be accessed using the scope resolution operator.  
void fun1 ( int n1)  
{  
// n1 is accessed by the scope resolution operator (:: )   
cout << " The value of the static integer n1: " << Parent::n1;  
cout << " \n The value of the local variable n1: " << n1;  
}  
};  
// define a static member explicitly using :: operator  
int Parent::n1 = 5; // declare the value of the variable n1  
int Parent::n2 = 10;      
int main ()  
{  
Parent b;  
int n1 = 15;  
b.fun1 (n1);  
cout << " \n The value of the Base::n2 = " << Parent::n2;  
return 0;  
}     

C++ LAB
Name: Ansh Gujral------Roll no.09------Uni.Roll No:2200709------BSc.IT. ------Sem2------

Output:
The value of the static integer n1: 5
The value of the local variable n1: 15
The value of the Base::n2 = 10

C++ LAB

You might also like