0% found this document useful (0 votes)
23 views16 pages

BCA-II Sem-IV CPP Journal 2024-25

The document contains a series of C++ programming exercises designed for a lab course in Object Oriented Programming. It includes examples of basic programming concepts such as operators, conditional statements, classes, access specifiers, constructors, inheritance, polymorphism, and virtual functions. Each exercise is accompanied by code snippets and their corresponding outputs.

Uploaded by

Akhil Kalagi
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)
23 views16 pages

BCA-II Sem-IV CPP Journal 2024-25

The document contains a series of C++ programming exercises designed for a lab course in Object Oriented Programming. It includes examples of basic programming concepts such as operators, conditional statements, classes, access specifiers, constructors, inheritance, polymorphism, and virtual functions. Each exercise is accompanied by code snippets and their corresponding outputs.

Uploaded by

Akhil Kalagi
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/ 16

Journal

in
Object Oriented Programming Using C++
Course Code: CCL 406
Lab Course-VII Based on CC401
BCA Part – II, Sem– IV
2024-25

1
1) Write a simple program (without Class) to use of operators in C++

#include <iostream>
#include<conio.h>
int main()
{
int a = 8, b = 3;
cout << "a + b = " << (a + b) << endl;
cout << "a - b = " << (a - b) << endl;
cout << "a * b = " << (a * b) << endl;
cout << "a / b = " << (a / b) << endl;
cout << "a % b = " << (a % b) << endl;
return 0;
}
/*Output
Output:- a + b = 11
a - b = 5
a * b = 24
a / b = 2
a % b = 2

*/

2
2) Write a if statement program :-
#include<iostream.h>
#include<conio.h>
Void main()
{
Clrscr();
int age=20;
if(age>=18)
{
Cout<<”are you eligible for voting ”<<endl;
}
getch();
}

/*output
are you eligible for voting
*/

3
3) Write a program to create a class and creating an object.
#include <iostream.h>
#include <string.h>
class MyClass
{
public:
int myNum;
string myString;
};
int main()
{
MyClass myObj;
myObj.myNum = 15;
myObj.myString = "Some text";
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}

/*output
50
Some text*/

4
4) Illustrating different Access Specifies.

#include<iostream.h>
#include<conio.h>
class Circle
{
public:
double radius;

double compute_area()
{
return 3.14*radius*radius;
}
};

int main()
{
Circle obj;
obj.radius = 5.5;
cout << "Radius is: " << obj.radius << "\n";
cout << "Area is: " << obj.compute_area();
return 0;
}

/*output
Radius is: 5.5
Area is: 94.985
*/

5
5 )Write a Loop program to demonstrate static data member

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

class A
{
public:
A()
{
cout << "A's Constructor Called " << endl;

}
};

class B
{
static A a;

public:
B()
{
cout << "B's Constructor Called " << endl;

}
};

int main()
{
B b;
return 0;
}

/*output
B's Constructor Called
*/

6
6 )Demonstrate arguments to the function.
#include <iostream.h>
#include <conio.h>

void myFunction(string fname)


{
cout << fname << " Refsnes\n";
}

int main()
{
myFunction("Liam");
myFunction("Jenny");
myFunction("Anja");
return 0;
}

/*output
Liam Refsnes
Jenny Refsnes
Anja Refsnes*/

7
7 )Illustrating inline function.
#include <iostream.h>
#include <conio.h>

inline int cube(int s)


{
return s * s * s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}

/*output
The cube of 3 is: 27

*/

8
8) Define Member function-outside the class using Scope Resolution
Operator

#include <iostream.h>
#include <conio.h>
class Operate
{
public:

void fun();
};
void Operate::fun()
{
cout << " It is the member function of the class. ";
}
int main ()
{
Operate op;
op.fun();
return 0;
}

/*output
It is the member function of the class.

*/

9
9) Illustrating friend class and friend function.
#include <iostream.h>
#include<conio.h>
class GFG
{
private:
int private_variable;

protected:
int protected_variable;

public:
GFG()
{
private_variable = 10;
protected_variable = 99;
}

// friend class declaration


friend class F;
};

class F
{
public:
void display(GFG& t)
{
cout << "The value of Private Variable = "
<< t.private_variable << endl;
cout << "The value of Protected Variable = "
<< t.protected_variable;
}
};

int main()
{
GFG g;
10
F fri;
fri.display(g);
return 0;
}

/*output

The value of Private Variable = 10


The value of Protected Variable = 99

*/

11
10 )Create constructors – default, parameterized, copy.
#include <iostream.h>
#include<conio.h>
class student
{
int rno;
char name[50];
double fee;

public:
student()
{
cout << "Enter the RollNo:";
cin >> rno;
cout << "Enter the Name:";
cin >> name;
cout << "Enter the Fee:";
cin >> fee;
}
void display()
{
cout << endl << rno << "\t" << name << "\t" << fee;
}
};
int main()
{
student s;
s.display();
return 0;
}
/*output

Enter the RollNo:121


Enter the Name:Geeks
Enter the Fee:5000
121 Geeks 5000 */

12
.
11) Illustrating Inheritance – single

#include <iostream.h>
#include <conio.h>
class Account
{
public:
float salary = 60000;
};
class Programmer: public Account
{
public:
float bonus = 5000;
};
int main(void)
{
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}

/*Output
Salary: 60000
Bonus: 5000
*/

13
12) Write a program static polymorphism
#include <iostream.h >
#include <conio.h >
class Geeks
{
public:
void func(int x)
{
cout << "value of x is " << x << endl;
}

void func(double x)
{
cout << "value of x is " << x << endl;
}
void func(int x, int y)
{
cout << "value of x and y is " << x << ", " << y
<< endl;
}
};
int main()
{
Geeks obj1;
obj1.func(7);
obj1.func(9.132);
obj1.func(85, 64);
return 0;
}
/*Output
value of x is 7
value of x is 9.132

value of x and y is 85, 64 */

14
13) Demonstrate on virtual program.
#include <iostream.h>
#include <conio.h>
class base
{
public:
virtual void print() { cout << "print base class\n";
}

void show() { cout << "show base class\n"; }


};

class derived : public base


{
public:
void print()
{
cout << "print derived class\n";
}

void show()
{
cout << "show derived class\n";
}
};

int main()
{ base* bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
return 0;
}
/*Output
print derived class
show base class

15
14) Demonstrate on pure virtual function
#include <iostream.h>
#include <conio.h>
class Base
{
int x;

public:
virtual void fun() = 0;

int getX()
{
return x;
}
};

class Derived : public Base


{
int y;

public:
void fun()
{
cout << "fun() called";
}
};

int main(void)
{
Derived d;
d.fun();
return 0;
}
/*Output
fun() called

*/
16

You might also like