Object Oriented Programming Practical 2

Question 1
Write a C++ program to access the global variable using scope resolution operator.
global.cpp
C
				#include<iostream.h>
#include<conio.h>

int num=100;

void main()
{
	clrscr();
	int num;
	cout<<"Enter number: ";
	cin>>num;
	cout<<"Value of number inside main is: "<<num<<endl;
	cout<<"Value of number outside main is: "<<::num<<endl;
	getch();
}

			
Output
Question 2
Format the following statement using manipulators.
(Rate=412345 period=35 year=2024 )
format.cpp
C
				#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <iomanip>

void main() 
{
    int rate = 412345;
    int period = 35;
    int year = 2024;

    cout << "Formatted Statement:\n";
    cout << "Rate   : " << ::setw(10) << ::setfill('0') << rate << '\n';
    cout << "Period : " << ::setw(10) << ::setfill(' ') << period << '\n';
    cout << "Year   : " << ::setw(10) << ::setfill(' ') << year << '\n';
    getch();
}

			
Output
Question 3
Write a C++ Program which show the use of function outside the class using scope resolution operator
expression2.cpp
C
				#include<iostream.h>
#include<conio.h>

class MyClass 
{
	public:
		void display();
};

void MyClass::display() {
    cout << "This function is outside class"<<endl;
}

void main() {
    MyClass obj;
    obj.display();
	getch();
}

			
Output
Question 4
Write a program to display the massage “Welcome to the world of C++” using manipulators.
welcome.cpp
C
				#include <iostream.h>
#include<conio.h>
#include <iomanip>

void main() 
{
    cout <<setw(40) <<setfill('*') << "Welcome to the world of C++" <<endl;
    getch();
}

			
Output
Question 5
Write a program to create the memory using new operator and free the created memory using delete operator.
memory.cpp
C
				#include <iostream.h>
#include <conio.h>

void main()
{
    int* ptr;
    ptr = new int;
    *ptr = 100;
    cout << "Address of new memory: " << ptr << endl;
    cout << "Value at memory: " << *ptr<<endl;
	delete ptr;
	cout << "Memory deleted";
    getch();
}


			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *