OOP x AI

Created by
BBorhan
Last edited time
Tag

Some Basics

Namespace

:::: → Scope Operator/Scope Resolution Operator

StructureClass
If access specifier is not declared explicitly, then by default access specifier will be public.If access specifier is not declared explicitly, then by default access specifier will be private.
Syntax of Structure
struct structure_name{// body of the structure.}
Syntax of Class
class class_name{// body of the class.}
The instance of the structure is known as "Structure variable".The instance of the class is known as "Object of the class".

OOP : Object Oriented Programming

Class

class myClass{
	int z; // private 

	public : 
		int x, y; // data member
		
		// member function
		int setter(int a, int b){
			x=a; y=b;
		}

		int show(){
				cout << x+y << "\n";
		}

		int functionCanBeWrittenFromOutside();
};

int myClass::functionCanBeWrittenFromOutside(){
	return x;
}

Objects

myClass ob1;
ob1.setter(1,2);
ob1.show();

Example:

#include<bits/stdc++.h>
using namespace std;

class Person{
	string name;
	string telephone;
	public:
	Person(string name, string telephone){
		this->name = name;
		this->telephone = telephone;
	}
	void show(){
		cout << "Name: " << this->name << "\n" << "Telehpone: " << this->telephone;
	}
};

int main(){
	string name, telephone; cin >> name >> telephone;
	Person *p = new Person(name, telephone);
	p->show();
}

Constructor

//Default or Non-parameterized Constructor
class amarClass{
	int x;
	public :
	amarClass(){
		cout << "Constructor" << "\n";
	} 
};
int main(){
	amarClass a;	
}


// Parameterized Constructor
class apnarClass{
	int x;
	public :
	//01
	apnarClass(int a, int b){
		x = a;
		cout << "Constructor " << a << " " << b << "\n";
	} 
	//02 Using default arguments
	apnarClass(int a=0){
		x = a;
		cout << a << " ";
	}
};
int main(){
	apnarClass b(8, 80);
	apnarClass c;  // 02 
}

//Copy Constructor
class array{
	int *p;
	int size;
	public:
...
		array(int sz){
				p = new int[sz];
				if(!p){
						exit(1);
				}
				size = sz;
		}
		array(const array &a){
			p = new int[a.size];
			if(!p) exit(1);
			size = a.size;
			for(int i=0; i<size) i++){
				p[i]=a.p[i];
			}
		}
		void put(int i, int j){
			if(i >= 0 && i < size){
					p[i]=j;
			}
		}
....
};
int main(){
...
	array obj(4);
	for(int i=0;' i<4; i++) put(i, i+8);
	array obj2(obj); // copy constructor
....
}

Destructor

class myClass{
	public :
	~myClass(){
		cout << "Destructor" << "\n";
	};
};

Home Work:

...
#include<ctime>
..
class stopwatch{
	clock_t start, end;
	clock_t object_start = 0;
	double elapsed_time;

	public:
	stopwatch(){
		elapsed_time = 0;
		object_start = clock();
	}
	~stopwatch(){
			end = clock();
			elapsed_time = (double) (object_start - end)/CLOCK_PER_SECOND;
			cout << elapsed_time << "\n";
	}
	void start(){
		start = clock();
	}

	void stop(){
		stop=clock();
	}

	void show(){
		elapsed_time = (double)(end-start)/CLOCK_PER_SECOND;
		cout << elapsed_time << "\n";	
	}
};

class box(){
	double a, b, c, volume;
	public:
	box(double x, double y, double z){
		a=x; b=y; c=z;
		volume = a*b*c;
	}
	void vol(){
		cout << volume << "\n";
	}
}

Inheritance

একটি প্রোগ্রামিং প্রক্রিয়া, যেখানে একটি ক্লাস আরেকটি ক্লাসের বৈশিষ্ট্যগুলি (members function, method) ব্যবহার করতে পারে।

Access Specifier
of Base Class
Public InheritancePrivate InheritanceProtected Inheritance
PublicPublicPrivateProtected
PrivateNot InheritedNot InheritedNot Inherited
ProtectedProtectedPrivateProtected
class base{
	int x, y, z;
	..
	public :
	..
	int sum(){
			z = x+y;
	}
	...
}

// Public Inheritance
class derived : public base{
....
}

// Private Inheritance
class derived2 : private base{
....
}

// Protected Inheritance
class derived3 : protected base{
....
}
  • Virtual Base Class

Derived3 class called “Base” class for two times to reduce this ambiguity we will use virtual class. There are two path two reach Base. If we use virtual base class , it will choose one path automatically. Otherwise, the compiler will visit the both path.

class base[
...
]
class deriverd1 : virtual public base{
...
}
class derived2 : virtual public base {
...
}
class derived3 : public deriver1, public derived2{
....
}

In-Line Function

inline int cube(int x){
	return x*x*x;
}
int main(){
...
int a=5;
int ans = cube(a);
....
}

#include <cstdlib>
#include <iostream>
#include <time.h>
...

class dice(){
	int val;
	public:
	void roll(){
		srand(time(0));
		val = rand()%6 + 1;
		cout << roll << "\n";
	}
}

...

Friend Functions

class myClass{
...
int x; // x is a private data member
...
public :
...

friend int isEven(myclass ob);
..
};

int isEven(myclass ob){
	return (ob.x%2==0);
}

int main(){
...
myClass obj;
...
cout << isEven(obj) << "\n";
...
}

Operator Overloading

Virtual Function

#include <bits/stdc++.h>
using namespace std;

class A{
	public:
		virtual void func(){
			cout << "A";
		}
};

class B:public A{
	public:
		void func(){
			cout << "B";
		}
};
int main() {
	A *p = new B();
	p->func();
}

OOP Characteristics

Function Overriding

class base {
...
public:
void func(){
	cout << "Base";
}
...
};

class derived : public base{
...
public:
void func() override{
	cout << "derived";
}
};

int main(){
	derived ob;
	ob.func(); // Output : Derived
}
// if we have to show the base class
class base {
...
public:
void func(){
	cout << "Base";
}
...
};

class derived : public base{
...
public:
void func() override{
	cout << "derived";
	// way 01:
	base::func();
}
};

int main(){
	derived ob;
	// Way : 02
	ob.Base::func(); // Output : Base
	// way : 03
	base ptr = new derived();
	ptr->func(); // Output : Base
}
class base{
	..
	public:
	void func(){
	....
	}
};

class derived{
	...
	public:
	void func(int a) override{
			.....
	}
};

int main(){
	derived a;
	a.func();
}
/*
Observation:
func() and func(int a) are not overriding, but overloading... by using "override" keyword, 
it shows an error, because the function isn't overriding..
to stop making this mistake, override keyword is used.
*/

Friend Class

Question and Answers

AspectObject-Oriented Programming (OOP)Procedural/Structured Programming
ParadigmBased on the concept of objects and classes.Based on procedures and functions.
Data and BehaviorEncapsulates data and behavior (methods/functions) together in classes.Separates data and behavior (functions) into different entities.
ModularityEmphasizes on creating reusable and modular code using classes and objects.Relies on functions and procedures for code modularity.
AbstractionSupports data abstraction using access control (public, private, protected).Limited or no direct support for data abstraction.
InheritanceSupports inheritance, allowing one class to derive properties from another.Typically does not support inheritance.
PolymorphismSupports polymorphism, enabling one interface (function) to work with different data types.Limited or no support for polymorphism.
EncapsulationEncapsulation is a fundamental principle, where data and methods are bundled together within a class.Encapsulation is not emphasized, and data may be freely accessed by different parts of the program.
ExamplesC++, Java, Python, etc.C, Pascal, Fortran, etc.
It generally follows “Bottom-Up Approach”.  It generally follows “Top-Down Approach”.  
It gives more importance to data. It gives more importance of code. 

Generic programming is a programming paradigm that aims to create reusable and flexible code by using templates or generics. In generic programming, algorithms and data structures are written in a way that they can work with different data types without having to rewrite the code for each specific type.

#include <iostream>

// Template function to find the maximum of two values
template <typename T>
T findMax(T a, T b) {
    return (a > b) ? a : b;
}

int main() {
    int intNum1 = 42, intNum2 = 73;
    double doubleNum1 = 3.14, doubleNum2 = 2.71;

    // Find the maximum of two integers
    int maxInt = findMax<int>(intNum1, intNum2);
    std::cout << "Maximum of " << intNum1 << " and " << intNum2 << " is " << maxInt << std::endl;

    // Find the maximum of two double values
    double maxDouble = findMax<double>(doubleNum1, doubleNum2);
    std::cout << "Maximum of " << doubleNum1 << " and " << doubleNum2 << " is " << maxDouble << std::endl;

    return 0;
}