Professional Documents
Culture Documents
Codes
Codes
#include <iostream>
using namespace std;
class Point {
public:
Point(int x, int y) : x(x), y(y) {}
private:
int x;
int y;
};
int main() {
Point p(3, 5);
return 0;
}
class Distance {
public:
Distance()
{
this->feet = 0;
this->inch = 0;
}
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;
return 0;
}
class Distance {
public:
int feet, inch;
Distance()
{
this->feet = 0;
this->inch = 0;
}
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
// Overloading (+) operator to perform addition of two distance
// object Call by reference
Distance operator+(Distance& d2)
{
// Create an object to return
Distance d3;
d3.feet = this->feet + d2.feet;
d3.inch = this->inch + d2.inch;
// Driver Code
int main()
{
Distance d1(8, 9);
Distance d2(10, 2);
Distance d3;
cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;
return 0;
}
class Distance {
public:
public:
int feet, inch;
// Driver Code
int main()
{
Distance d1(8, 9);
#include <iostream>
using namespace std;
class MyClass {
public:
int data;
// Constructor
MyClass(int value) : data(value) {}
int main() {
MyClass obj(0);
// Using the overloaded >> operator to input data into the object
cout << "Enter a value: ";
cin >> obj;
return 0;
}
#include <iostream>
using namespace std;
class Point {
public:
Point(int x, int y) : x(x), y(y) {}
private:
int x;
int y;
};
int main() {
Point p(3, 5);
return 0;
}
#include<iostream>
using namespace std;
class OpeDemo
{
public:
string str;
OpeDemo(string s)
{
str= s;
}
#include<iostream>
using namespace std;
class OpeDemo
{
public:
string str;//data member
OpeDemo(string s)//constructor
{
str= s;
}
int main()
{
OpeDemo obj1("SREE");
OpeDemo obj2("VARSHA");
OpeDemo obj3 = obj1 + obj2;
cout << obj3.str << endl; // Print the concatenated string.
}
#include<iostream>
using namespace std;
class Demo
{
public:
}
};
int main()
{
int a,b;
cout<<"Enter a and b values:"<<endl;
cin>>a>>b;
Demo obj;//object creation
Demo::add(a,b);
obj.sub(a,b);
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
std::vector<int> squaredNumbers(numbers.size());
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Declare a vector of integers
vector<int> myVector ;
// Vector size
cout << "\nVector size: " << myVector.size() << endl;
return 0;
}
#include <iostream>
using namespace std;
public:
ClassTemplate(T v1, U v2, V v3) : var1(v1), var2(v2), var3(v3) {} // constructor
void printVar() {
cout << "var1 = " << var1 << endl;
cout << "var2 = " << var2 << endl;
cout << "var3 = " << var3 << endl;
}
};
int main() {
// create object with int, double and char types
ClassTemplate<int, double> obj1(7, 7.7, 'c');
cout << "obj1 values: " << endl;
obj1.printVar();
#include <iostream>
using namespace std;
public:
Calculator(T n1, T n2) {
num1 = n1;
num2 = n2;
}
void displayResult() {
cout << "Numbers: " << num1 << " and " << num2 << "." << endl;
cout << num1 << " + " << num2 << " = " << add() << endl;
cout << num1 << " - " << num2 << " = " << subtract() << endl;
cout << num1 << " * " << num2 << " = " << multiply() << endl;
cout << num1 << " / " << num2 << " = " << divide() << endl;
}
int main() {
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
cout << "Int results:" << endl;
intCalc.displayResult();
return 0;
}
#include <iostream>
using namespace std;
// Class template
template <class T>
class Number {
private:
// Variable of type T
T num;
public:
Number(T n) : num(n) {} // constructor
T getNum() {
return num;
}
};
int main() {
return 0;
}
#include <iostream>
using namespace std;
int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout <<result1 << endl;
return 0;
}
//aggregation demo
#include <iostream>
#include <string>
class Department {
public:
Department(std::string name) : name(name) {
}
private:
std::string name;
};
class University {
public:
University(std::string name) : name(name), departmentCount(0), departments(NULL) {
}
void displayDepartments() {
std::cout << "Departments at " << name << ":\n";
for (int i = 0; i < departmentCount; ++i) {
std::cout << " - " << departments[i]->getName() << "\n";
}
}
~University() {
for (int i = 0; i < departmentCount; ++i) {
delete departments[i];
}
delete[] departments;
}
private:
std::string name;
int departmentCount;
Department** departments; // Aggregation: University has Departments (pointers)
};
int main() {
Department dept1("Computer Science");
Department dept2("Physics");
Department dept3("Mathematics");
myUniversity.displayDepartments();
return 0;
}
#include <iostream>
using namespace std;
// base class
class Base_class
{
public:
int x;
};
// class 1
class class_1 : public Base_class
{
public:
int y;
};
// class 2
// derived class 3
int main()
{
// create an object of the derived_class
derived_class obj;
obj.class_1::x = 10;//
obj.y = 20;
obj.z = 30;
cout << "The sum is: " << obj.sum << "\n\n";
return 0;
#include <iostream>
using namespace std;
// base class
class Base_class
{
public:
int x;
};
// class 1
class class_1 : virtual public Base_class
{
public:
int y;
};
// class 2
class class_2 : virtual public Base_class
{
public:
int z;
};
// derived class 3
class derived_class : public class_1, public class_2
{
public:
int sum;
};
int main()
{
// create an object of the derived_class
derived_class obj;
obj.x = 10; // it is now unambiguous
obj.y = 20;
obj.z = 30;
obj.sum = obj.x + obj.y + obj.z;
cout << "The sum is: " << obj.sum << "\n\n";
return 0;
}
private:
//class member
int base_private;
protected:
//class member
int base_protected;
public:
//class member
int base_public;
};
private:
int derived_private;
// int base_private;
// int base_protected;
// int base_public
protected:
int derived_protected;
public:
int derived_public;
};
int main()
derived_class obj;
private:
//class member
int base_private;
protected:
//class member
int base_protected;
public:
//class member
int base_public;
};
private:
int derived_private;
// int base_private;
protected:
int derived_protected;
// int base_protected;
// int base_public
public:
int derived_public;
};
int main()
{
// Accessing members of base_class using object of the derived_class:
derived_class obj;
class base_class
private:
//class member
int base_private;
protected:
//class member
int base_protected;
public:
//class member
int base_public;
};
private:
int derived_private;
// int base_private;
protected:
int derived_protected;
// int base_protected;
public:
int derived_public;
// int base_public;
};
int main()
{
derived_class obj;
obj.base_public; // Accessible
//Inheritance demo
#include <iostream>
using namespace std;
// base class
class Animal
{
public:
// constructor of the base class
Animal()
{
cout << "I am an animal.-Base class\n";
}
};
// derived class
class Pet: public Animal
{
public:
// constructor of the derived class
Pet()
{
cout << "I am a pet.-Derived class\n";
}
};
int main()
{
// create an object of the child's class
Pet obj;
return 0;
}