INDEX
Sr. Page.
Particular Date Sign
No No
Write a simple program (without Class) to use of
1
operators in C++.
2 Illustrating control structure.
Write a program to create a class and creating an
3
object.
4 Illustrating different Access Specifiers.
Write a OOP program to demonstrate static data
5
member
6 Demonstrate arguments to the function.
7 Illustrating inline function.
Define member function-outside the class using
8
scope resolution operator.
9 Illustrating friend class and friend function.
10 Create constructors – default, parameterized, copy.
11 Destructor.
12 Dynamic Initialization of Object.
Illustrating Inheritance – Single, Multiple, and
13
Multilevel.
14 Perform statics and dynamic Polymorphism.
15 Demonstrate virtual & pure virtual function.
1
Assignment no. 1
Q. Write a simple program (without Class) to use of operators in C++.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int
a,b;
a=10;
b=5;
cout<<"Value of a="<<a<<endl;
cout<<"Value of b="<<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;
cout<<"a%b="<< (a%b) <<endl;
getch();
return 0;
}
2
Assignment no. 2
Q. Illustrating Control Structures.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int n;
cout<<"Enter a positive integer:";
cin>>n;
for(int i=1;i<=10;++i)
{
cout<<n<<"*"<<i<<"="<<n*i<<endl;
}
getch();
return 0;
}
3
Assignment no. 3
Q. Write a program to create a class and creating an object.
#include<iostream.h>
#include<conio.h>
class Room
{
public:
double
length;
double
breadth;
double
height;
double calculateArea()
{
return length * breadth;
}
double calculateVolume()
{
return length * breadth * height;
}
};
int main()
{
Room room1;
[Link]=42.5;
[Link]=30.8;
[Link]=19.2;
cout<<"Length = "<<[Link]<<endl;
4
cout<<"Breadth = "<<[Link]<<endl;
cout<<"Height = "<<[Link]<<endl;
cout<<"Area of Room="<< [Link]() <<endl;
cout<<"Area of Room="<< [Link]() <<endl;
getch();
return 0;
}
5
Assignment no.4
Q. Illustrating different Access Specifiers.
#include<iostream.h>
#include<conio.h>
class Sample
{
private:
int age;
public:
void displayAge(int a)
{
age=a;
cout<<"Age="<<age<<endl;
}
};
int main()
{
clrscr();
int ageinput;
Sample obj1;
cout<<"Enter your age: ";
cin>>ageinput;
[Link](ageinput);
getch();
return 0;
}
6
Assignment no.5
Q. Write a OOP program to demonstrate static data member.
#include<iostream.h>
#include<conio.h>
void test()
{
int var=0;
var++;
cout<<"Static Variable Value = "<<var<<endl;
}
int main()
{
clrscr();
test();
test();
test();
getch();
return 0;
}
7
Assignment no. 6
Q. Demonstrate arguments to the function.
#include<iostream.h>
#include<conio.h>
int add(int a, int b)
{
return(a+b);
}
void displayMessage(const string& message)
{
cout << "Message from function: " << message << endl;
}
int main()
{
clrscr();
int sum;
sum=add(100, 50);
cout<<"100 + 50 = "<<sum<<endl;
string greeting = "Hello, World!";
displayMessage(greeting);
getch();
return 0;
}
8
Assignment no. 7
Q. Illustrating inline function.
#include <iostream.h>
#include<conio.h>
// Inline function to calculate the square of a number
inline int square(int x) {
return x * x;
}
int main() {
int num;
clrscr();
cout << "Enter a number: ";
cin >> num;
// Calling the inline function
int result = square(num);
cout << "Square of " << num << " is: " << result << endl;
getch();
return 0;
}
9
Assignment no. 8
Q. Define Member function-outside the class using Scope Resolution
Operator.
#include <iostream.h>
#include<conio.h>
class MyClass {
private:
int myData;
public:
void setData(int data);
void displayData();
};
void MyClass::setData(int data) {
myData = data;
}
void MyClass::displayData() {
cout << "Data: " << myData << endl;
}
int main() {
MyClass obj;
clrscr();
[Link](50);
[Link]();
getch();
return 0;
}
10
Assignment no. 9
Q. Illustrating friend class and friend function.
#include <iostream.h>
#include<conio.h>
// Forward declaration of FriendClass
class FriendClass;
// Class with private and protected members
class MyClass {
private:
int privateData;
protected:
int protectedData;
public:
MyClass(int privateValue, int protectedValue) : privateData(privateValue), protectedData(protectedValue)
{}
// Declaration of friend function
friend void friendFunction(const MyClass&);
// Declaration of friend class
friend class FriendClass;
};
// Friend function definition
void friendFunction(const MyClass& obj) {
cout << "Friend Function: Accessing privateData = " << [Link] << ", protectedData = " <<
[Link] << endl;
}
// Friend class definition
class FriendClass {
public:
void accessPrivateAndProtected(const MyClass& obj) {
cout << "Friend Class: Accessing privateData = " << [Link] << ", protectedData = " <<
[Link] << endl;
}
};
int main() {
MyClass obj(10, 20);
clrscr();
// Accessing using friend function
friendFunction(obj);
11
// Accessing using friend class
FriendClass friendObj;
[Link](obj);
getch();
return 0;
}
12
Assignment no. 10
Q. Create constructors – default, parameterized, copy.
#include <iostream.h>
#include<conio.h>
class MyClass {
private:
int data;
public:
// Default constructor
MyClass() {
data = 0;
cout << "Default Constructor called." << endl;
}
// Parameterized constructor
MyClass(int value) {
data = value;
cout << "Parameterized Constructor called with value: " << value << endl;
}
// Copy constructor
MyClass(const MyClass &other) {
data = [Link];
cout << "Copy Constructor called. Copied data from another object." << endl;
}
// Member function to display data
void displayData() {
13
cout << "Data: " << data << endl;
}
};
int main() {
// Using default constructor
MyClass obj1;
clrscr();
[Link]();
// Using parameterized constructor
MyClass obj2(42);
[Link]();
// Using copy constructor
MyClass obj3 = obj2; // or MyClass obj3(obj2);
[Link]();
getch();
return 0;
}
14
Assignment no. 11
Q. Destructor.
#include <iostream.h>
#include<conio.h>
class Employee {
public:
Employee() {
cout<<"Constructor Invoked"<<endl;
}
~Employee() {
cout<<"Destructor Invoked"<<endl;
}
};
int main(void) {
clrscr();
Employee e1;
Employee e2;
getch();
return 0;
}
15
Assignment no. 12
Q. Dynamic Initialization of Object.
#include<iostream.h>
#include<conio.h>
struct student
{
private:
int rno;
float perc;
public:
student(int r,float p)
{
rno= r;
perc =p;
}
void read(void)
{
cout<<"enter roll number:"<<endl;
cin>>rno;
cout<<"enter percentage:" << endl;
cin>>perc;
}
void print(void)
{
cout<<endl;
cout<<"roll number:"<<rno<<endl;
cout<<"percentage:"<<perc<<"%"<<endl;
}
};
int main()
{
clrscr();
16
cout<<"enter roll number to initialize the object:";
int roll_number;
cin>>roll_number;
cout<<"enter percentage to initialize the object:";
float percentage;
cin>>percentage;
struct student std(roll_number,percentage);
cout<<"after initialize the object,the values are..."<<endl;
[Link]();
[Link]();
getch();
return 0;
}
17
Assignment no. 13
Q. Illustrating Inheritance – Single, Multiple, and Multilevel.
#include <iostream.h>
#include<conio.h>
// Base class for single inheritance
class Animal {
public:
void eat() {
cout << "Animal is eating." << endl;
}
};
// Derived class for single inheritance
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking." << endl;
}
};
// Base classes for multiple inheritance
class Flying {
public:
void fly() {
cout << "Flying high in the sky." << endl;
}
};
class Swimming {
public:
void swim() {
cout << "Swimming in the water." << endl;
18
}
};
// Derived class for multiple inheritance
class Duck : public Flying, public Swimming {
public:
void quack() {
cout << "Duck is quacking." << endl;
}
};
// Base class for multilevel inheritance
class Mammal {
public:
void giveBirth() {
cout << "Mammal is giving birth." << endl;
}
};
// Derived class for multilevel inheritance
class Dolphin : public Mammal {
public:
void swim() {
cout << "Dolphin is swimming." << endl;
}
};
// Further derived class for multilevel inheritance
class IntelligentDolphin : public Dolphin {
public:
void solveProblems() {
cout << "Intelligent Dolphin is solving problems." << endl;
}
19
};
int main() {
clrscr();
// Single Inheritance
Dog myDog;
[Link]();
[Link]();
// Multiple Inheritance
Duck myDuck;
[Link]();
[Link]();
[Link]();
// Multilevel Inheritance
IntelligentDolphin smartDolphin;
[Link]();
[Link]();
[Link]();
getch();
return 0;
}
20
Assignment no. 14
Q. Perform statics and dynamic Polymorphism.
• Static Polymorphism :
#include <iostream>
class Shape {
public:
// Function Overloading
double calculateArea(int side) {
return side * side; // Area of a square
}
double calculateArea(double radius) {
return 3.14 * radius * radius; // Area of a circle
}
};
int main() {
Shape shape;
// Static polymorphism through function overloading
cout << "Area of Square: " << [Link](5) << endl;
cout << "Area of Circle: " << [Link](2.5) << endl;
return 0;
}
21
• Dynamic Polymorphism :
#include <iostream>
// Base class with a virtual function
class Animal {
public:
// Virtual function
virtual void makeSound() {
std::cout << "Animal makes a generic sound." << std::endl;
}
};
// Derived classes overriding the virtual function
class Dog : public Animal {
public:
// Override the virtual function
void makeSound() override {
std::cout << "Dog barks." << std::endl;
}
};
class Cat : public Animal {
public:
// Override the virtual function
void makeSound() override {
std::cout << "Cat meows." << std::endl;
}
};
int main() {
// Dynamic polymorphism through function overriding
Animal* dog = new Dog();
22
Animal* cat = new Cat();
dog->makeSound(); // Output: Dog barks.
cat->makeSound(); // Output: Cat meows.
delete dog;
delete cat;
return 0;
}
23
Assignment no. 15
Q. Demonstrate virtual & pure virtual function.
#include <iostream>
#include <cmath>
// Base class with a virtual function
class Shape {
public:
// Virtual function
virtual void draw() const {
std::cout << "Drawing a generic shape." << std::endl;
}
// Pure virtual function
virtual double calculateArea() const = 0; // Pure virtual function
};
// Derived classes implementing the virtual and pure virtual functions
class Circle : public Shape {
private:
double radius;
public:
// Constructor
Circle(double r) : radius(r) {}
// Override the virtual function
void draw() const override {
std::cout << "Drawing a circle." << std::endl;
}
24
// Implement the pure virtual function
double calculateArea() const override {
return 3.14 * radius * radius; // Area of a circle
}
};
class Square : public Shape {
private:
double side;
public:
// Constructor
Square(double s) : side(s) {}
// Override the virtual function
void draw() const override {
std::cout << "Drawing a square." << std::endl;
}
// Implement the pure virtual function
double calculateArea() const override {
return side * side; // Area of a square
}
};
class Triangle : public Shape {
private:
double base;
double height;
public:
25
// Constructor
Triangle(double b, double h) : base(b), height(h) {}
// Override the virtual function
void draw() const override {
std::cout << "Drawing a triangle." << std::endl;
}
// Implement the pure virtual function
double calculateArea() const override {
return 0.5 * base * height; // Area of a triangle
}
};
int main() {
// Using dynamic binding for polymorphism
Shape* shapes[3] = {new Circle(5.0), new Square(4.0), new Triangle(3.0, 6.0)};
for (const auto& shape : shapes) {
// Calls the overridden virtual function based on the actual type of the object
shape->draw();
// Calls the pure virtual function implemented in the derived class
std::cout << "Area of the shape: " << shape->calculateArea() << std::endl;
delete shape;
}
return 0;
}
26