OBJECT ORIENTED PROGRAMMING FILE
UNIVERSITY INSTITUTE OF ENGINEERING AND
TECHNOLOGY, KURUKSHETRA,
KURUKSHETRA UNIVERSITY
Submi ed to: Submi ed by:
Mrs. Poonam RONAK
Associate Professor BTech ECO(4th sem)
Dept. of CSE 252305038
Program 1
WAP to find the sum of individual digits of a posi ve integer.
#include <iostream>
using namespace std;
int sumOfDigits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
int num;
cout << "Enter a posi ve integer: ";
cin >> num;
cout << "Sum of digits: " << sumOfDigits(num) << endl;
return 0;
}
Program 2
WAP to generate the first n terms of the sequence.
#include <iostream>
using namespace std;
void printFibonacci(int n) {
int a = 0, b = 1;
cout << "Fibonacci sequence: ";
for (int i = 1; i <= n; ++i) {
cout << a << (i < n ? ", " : "\n");
int next = a + b;
a = b;
b = next;
}
}
int main() {
int n;
cout << "Enter number of terms: ";
cin >> n;
printFibonacci(n);
return 0;
}
Program 3
WAP to implement class with encapsula on.
#include <iostream>
using namespace std;
class Encapsulated {
private:
int value;
public:
void setValue(int v) {
if (v >= 0) value = v;
}
int getValue() const {
return value;
}
};
int main() {
Encapsulated obj;
obj.setValue(10);
cout << "Encapsulated value: " << obj.getValue() << endl;
return 0;
}
Program 4
WAP to implement access specifiers.
#include <iostream>
using namespace std;
class AccessDemo {
public:
int pub;
protected:
int prot;
private:
int priv;
public:
AccessDemo() : pub(1), prot(2), priv(3) {}
void show() {
cout << "public: " << pub << ", protected: " << prot << ", private:
" << priv << endl;
}
};
int main() {
AccessDemo a;
a.pub = 10; // OK
// a.prot = 20; // Error: protected
// a.priv = 30; // Error: private
a.show();
return 0;
}
Program 5
WAP to illustrate New and Delete Keywords for dynamic
memory alloca on.
#include <iostream>
using namespace std;
int main() {
int *ptr = new int; // dynamic alloca on
*ptr = 100;
cout << "Value: " << *ptr << endl;
delete ptr; // dealloca on
int n;
cout << "Enter size of array: ";
cin >> n;
int *arr = new int[n]; // dynamic array
for (int i = 0; i < n; ++i) arr[i] = i+1;
cout << "Array elements: ";
for (int i = 0; i < n; ++i) cout << arr[i] << " ";
cout << endl;
delete[] arr; // dealloca on of array
return 0;
}
Program 6
WAP to implement default constructor, parameterized
constructor and copy constructors.
#include <iostream>
using namespace std;
class Example {
private:
int x;
public:
Example() : x(0) { cout << "Default constructor\n"; }
Example(int v) : x(v) { cout << "Parameterized constructor\n"; }
Example(const Example &e) : x(e.x) { cout << "Copy constructor\n";
}
void show() { cout << "x = " << x << endl; }
};
int main() {
Example e1; // default
e1.show();
Example e2(42); // parameterized
e2.show();
Example e3 = e2; // copy
e3.show();
return 0;
}
Program 7
WAP to implement inheritance.
#include <iostream>
using namespace std;
class Base {
public:
void baseFunc() {
cout << "Func on of Base" << endl;
}
};
class Derived : public Base {
public:
void derivedFunc() {
cout << "Func on of Derived" << endl;
}
};
int main() {
Derived d;
d.baseFunc();
d.derivedFunc();
return 0;
}
Program 8
WAP to implement types of inheritance.
#include <iostream>
using namespace std;
// Single Inheritance
class A {
public:
void showA() { cout << "Class A" << endl; }
};
class B : public A {
public:
void showB() { cout << "Class B" << endl; }
};
// Mul ple Inheritance
class C {
public:
void showC() { cout << "Class C" << endl; }
};
class D : public A, public C {
public:
void showD() { cout << "Class D" << endl; }
};
// Mul level Inheritance
class E : public B {
public:
void showE() { cout << "Class E" << endl; }
};
// Hierarchical Inheritance
class F : public A {
public:
void showF() { cout << "Class F" << endl; }
};
class G : public A {
public:
void showG() { cout << "Class G" << endl; }
};
int main() {
cout << "Single Inheritance " << endl;
B b;
b.showA(); b.showB();
cout << "\n Mul ple Inheritance " << endl;
D d;
d.showA(); d.showC(); d.showD();
cout << "\n Mul level Inheritance " << endl;
E e;
e.showA(); e.showB(); e.showE();
cout << "\n Hierarchical Inheritance " << endl;
F f;
G g;
f.showA(); f.showF();
g.showA(); g.showG();
return 0;
}