├── + operator overloading ├── Append data into file ├── BankingProgram ├── Basic To Class. Conversion ├── Car Class with :: ├── Class to Basic conversion ├── Class to class conversion ├── Copy Constructor ├── Default Constructor ├── Destructor ├── Dynamic Memory Allocation ├── File input output ├── Friend Function ├── Friend-Class ├── Inheritance in C++ ├── Inheritance-Single ├── Modify base class data member using object refrence ├── Multiple Inheritance ├── Mutable Storage Class ├── Nested template class ├── Nested-Class ├── New and arrow operator ├── Not using virtual function ├── Overloading - operator ├── Parameterised constructor ├── Pure VF ├── Pure Virtual function ├── Put() and Insertion operator(<<) ├── Returning Object From Function ├── Student Class With Public and Private ├── Student Class wit scope resol operator ├── Student-Class ├── Template class ├── Template function overloading ├── Virtual Base Class ├── constructor overloading ├── extraction operator (>>) ├── namespace in C++ ├── operator overloading using friend function ├── this keyword in c++ ├── virtual Function In C++ └── virtual function and method overriding /+ operator overloading: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Base { 4 | private: 5 | double x, y; 6 | 7 | public: 8 | Base(double dx, double dy) 9 | { 10 | x=dx; 11 | y=dy; 12 | } 13 | 14 | double getX() 15 | { 16 | return x; 17 | } 18 | double getY() 19 | { 20 | return y; 21 | } 22 | 23 | // Overload the + operator using a friend function 24 | friend Base operator+(Base& v1,Base& v2) { 25 | return Base(v1.x + v2.x, v1.y + v2.y); 26 | } 27 | }; 28 | 29 | int main() { 30 | Base v1{ 3.0, 4.0 }; 31 | Base v2{ 1.0, 2.0 }; 32 | Base v3 = v1 + v2; 33 | 34 | cout << "v1: (" << v1.getX() << ", " << v1.getY() << ")\n"; 35 | cout << "v2: (" << v2.getX() << ", " << v2.getY() << ")\n"; 36 | cout << "v3: (" << v3.getX() << ", " << v3.getY() << ")\n"; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Append data into file: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | // Function to create a file and write to it 6 | void createAndWriteFile(const string& filename, const string& content) { 7 | ofstream file(filename); 8 | if (file.is_open()) { 9 | file << content; 10 | file.close(); 11 | cout << "File created and written successfully.\n"; 12 | } else { 13 | cout << "Unable to open file for writing.\n"; 14 | } 15 | } 16 | 17 | // Function to read from a file 18 | void readFile(const string& filename) { 19 | ifstream file(filename); 20 | string line; 21 | if (file.is_open()) { 22 | while (getline(file, line))//getline 23 | { 24 | cout << line << '\n'; 25 | } 26 | file.close(); 27 | } else { 28 | cout << "Unable to open file for reading.\n"; 29 | } 30 | } 31 | 32 | // Function to append to a file 33 | void appendToFile(const string& filename, const std::string& content) { 34 | std::ofstream file(filename, std::ios::app); 35 | if (file.is_open()) { 36 | file << content; 37 | file.close(); 38 | std::cout << "Content appended successfully.\n"; 39 | } else { 40 | std::cout << "Unable to open file for appending.\n"; 41 | } 42 | } 43 | 44 | int main() { 45 | std::string filename = "example.txt"; 46 | std::string content = "Hello, World!"; 47 | 48 | // Create and write to a file 49 | createAndWriteFile(filename, content); 50 | 51 | // Read from the file 52 | readFile(filename); 53 | 54 | // Append to the file 55 | appendToFile(filename, "\nThis is an appended line."); 56 | 57 | // Read from the file again to see the appended content 58 | readFile(filename); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /BankingProgram: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class bank 8 | { 9 | int accno; 10 | char nm[100], acctype[100]; 11 | float bal; 12 | public: 13 | bank(int acc_no, char *name, char *acc_type, float balance) //Parameterized Constructor 14 | { 15 | accno=acc_no; 16 | strcpy(nm, name); 17 | strcpy(acctype, acc_type); 18 | bal=balance; 19 | } 20 | void deposit(); 21 | void withdraw(); 22 | void display(); 23 | }; 24 | void bank::deposit() //depositing an amount 25 | { 26 | int damt1; 27 | cout<<"\n Enter Deposit Amount = "; 28 | cin>>damt1; 29 | bal+=damt1; 30 | } 31 | void bank::withdraw() //withdrawing an amount 32 | { 33 | int wamt1; 34 | cout<<"\n Enter Withdraw Amount = "; 35 | cin>>wamt1; 36 | if(wamt1>bal) 37 | cout<<"\n Cannot Withdraw Amount"; 38 | bal-=wamt1; 39 | } 40 | void bank::display() //displaying the details 41 | { 42 | cout<<"\n ----------------------"; 43 | cout<<"\n Accout No. : "<>acc_no; 57 | cout<<"\n Name : "; 58 | cin>>name; 59 | cout<<"\n Account Type : "; 60 | cin>>acc_type; 61 | cout<<"\n Balance : "; 62 | cin>>balance; 63 | 64 | bank b1(acc_no, name, acc_type, balance); //object is created 65 | b1.deposit(); // 66 | b1.withdraw(); // calling member functions 67 | b1.display(); // 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Basic To Class. Conversion: -------------------------------------------------------------------------------- 1 | //Basic to class conversion 2 | #include 3 | using namespace std; 4 | 5 | class Time { 6 | int hrs, min; 7 | 8 | public: 9 | Time(int); 10 | void display(); 11 | }; 12 | 13 | Time::Time(int t) { 14 | cout << "Basic Type to ==> Class Type Conversion..." << endl; 15 | hrs = t / 60; 16 | min = t % 60; 17 | } 18 | 19 | void Time::display() { 20 | cout << hrs << ": Hours(s)" << endl; 21 | cout << min << " Minutes" << endl; 22 | } 23 | 24 | int main() { 25 | int duration; 26 | cout << "Enter time duration in minutes: "; 27 | cin >> duration; 28 | 29 | Time t1 = duration; 30 | t1.display(); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Car Class with ::: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // USERFUN 4 | // 5 | // Created by Nilesh Taware on 25/12/23. 6 | // 7 | #include 8 | using namespace std; 9 | class Car 10 | { 11 | 12 | string car_model; 13 | string car_name; 14 | string car_color; 15 | int car_mfg; 16 | public: 17 | void input_car_details(); 18 | void display_car_details(); 19 | 20 | }; 21 | 22 | void Car::input_car_details() 23 | { 24 | cout<<"Please enter car details:"<>car_model; 27 | cout<<"Car Name:"<>car_name; 29 | cout<<"Car Colour"<>car_color; 31 | cout<<"Car Mfg Year:"<>car_mfg; 33 | } 34 | void Car::display_car_details() 35 | { 36 | cout<<"---Car Details--- "< 3 | using namespace std; 4 | class Time { 5 | int hrs, min; 6 | 7 | public: 8 | Time(int, int); 9 | operator int(); 10 | int getHrs(); 11 | int getMins(); 12 | }; 13 | 14 | Time::Time(int t1, int t2) { 15 | hrs = t1; 16 | min = t2; 17 | } 18 | 19 | Time::operator int() { 20 | return hrs * 60 + min; 21 | } 22 | 23 | int Time::getHrs() { 24 | return hrs; 25 | } 26 | int Time::getMins() 27 | { 28 | return min; 29 | } 30 | 31 | int main() { 32 | Time t(3, 40); 33 | int duration; 34 | duration = t; 35 | cout << "Hour: " << t.getHrs() << endl; 36 | cout << "Minute: " << t.getMins() << endl; 37 | cout << "Total Minutes: " << duration << endl; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Class to class conversion: -------------------------------------------------------------------------------- 1 | //class to class conversion 2 | #include 3 | using namespace std; 4 | class student { 5 | public: 6 | int rollno; 7 | student(int stud) { 8 | rollno = stud; 9 | } 10 | }; 11 | 12 | class university { 13 | public: 14 | int data; 15 | university(student &tempvar) { 16 | data = tempvar.rollno; 17 | } 18 | void display() 19 | { 20 | cout<<"University Data:"< 2 | using namespace std; 3 | 4 | // declare a class 5 | class Car { 6 | private: 7 | double length; 8 | double height; 9 | 10 | public: 11 | 12 | // initialize variables with parameterized constructor 13 | Car(double len, double hgt) { 14 | length = len; 15 | height = hgt; 16 | } 17 | 18 | // copy constructor with a car object as parameter 19 | // copies data of the obj parameter 20 | Car(Car &obj) { 21 | length = obj.length; 22 | height = obj.height; 23 | } 24 | 25 | double calculateArea() { 26 | return length * height; 27 | } 28 | }; 29 | int main() { 30 | // create an object of Car1 class 31 | Car car1(10.5, 8.6); 32 | 33 | // copy contents of car1 to car2 34 | Car car2 = car1; 35 | 36 | // print areas of car1 and car2 37 | cout << "Area of Car 1: " << car1.calculateArea() << endl; 38 | cout << "Area of Car 2: " << car2.calculateArea(); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Default Constructor: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Dept 4 | { 5 | int dept_id; 6 | string dept_name; 7 | public: 8 | Dept(); 9 | void display(); 10 | }; 11 | Dept:: Dept() 12 | { 13 | dept_id=111; 14 | dept_name="Computer Science"; 15 | 16 | } 17 | void Dept::display() 18 | { 19 | cout< 3 | using namespace std; 4 | class Base 5 | { 6 | public: 7 | Base() // Constructor function. 8 | { 9 | cout<< "\n Constructor Base class"; 10 | } 11 | ~Base() // Destructor function 12 | { 13 | cout<< "\n Destructor Base class"; 14 | } 15 | }; 16 | 17 | class Derived: public Base 18 | { 19 | public: 20 | Derived() // Constructor function 21 | { 22 | cout << "\n Constructor Derived class" ; 23 | } 24 | ~Derived() // Destructor function 25 | { 26 | cout << "\n Destructor Derived class" ; //Destructor function is not called to //release its space. 27 | } 28 | }; 29 | int main() 30 | { 31 | Base *bptr = new Derived; // Create a base class pointer object 32 | delete bptr; 33 | // Here pointer object is called to delete the //space occupied by the destructor. 34 | } 35 | -------------------------------------------------------------------------------- /Dynamic Memory Allocation: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Person { 4 | private: 5 | string person_name; 6 | int person_age; 7 | public: 8 | Person(const string& name, int age) 9 | { 10 | person_name=name; 11 | person_age=age; 12 | } 13 | 14 | void displayInfo() const { 15 | cout << "Name: " << person_name; 16 | cout<<" "<<"Age:"<displayInfo(); 25 | 26 | // Don't forget to deallocate the memory using the delete operator 27 | delete person; 28 | //try to access data related to function after delete 29 | person->displayInfo(); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /File input output: -------------------------------------------------------------------------------- 1 | //file input/output 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main () { 8 | // Create a text file 9 | ofstream MyWriteFile("/Users/nileshtaware/Desktop/USERFUN/USERFUN/example.txt"); 10 | 11 | // Write to the file 12 | MyWriteFile << "File handaling in c++"; 13 | 14 | // Close the file 15 | MyWriteFile.close(); 16 | 17 | // Create a text string, which is used to output the text file 18 | string myText; 19 | 20 | // Read from the text file 21 | ifstream MyReadFile("/Users/nileshtaware/Desktop/USERFUN/USERFUN/example.txt"); 22 | 23 | // Use a while loop together with the getline() function to read the file line by line 24 | while (getline (MyReadFile, myText)) { 25 | // Output the text from the file 26 | cout << myText< 2 | using namespace std; 3 | class Base; //forward decl 4 | class Height 5 | { 6 | int num1; 7 | public: 8 | void setvalue(int x) 9 | { 10 | num1=x;; 11 | } 12 | friend void operation(Base,Height); 13 | 14 | }; 15 | class Base{ 16 | int num2; 17 | public: 18 | void getvalue(int y) 19 | { 20 | num2=y; 21 | } 22 | friend void operation(Base,Height); 23 | 24 | }; 25 | void operation(Base b,Height h) 26 | { 27 | cout<<"Addition="< 2 | using namespace std; 3 | class University; 4 | class Student 5 | { 6 | private: 7 | int stud_roll=22; 8 | int stud_marks=99.99; 9 | protected: 10 | string stud_name="Krishna"; 11 | friend class University; 12 | 13 | 14 | }; 15 | class University 16 | { 17 | public: 18 | void get_details(Student& stud ) 19 | { 20 | cout< 3 | using namespace std; 4 | 5 | class Polygon { 6 | protected: 7 | int width, height; 8 | public: 9 | void set_values (int a, int b) 10 | { 11 | width=a; 12 | height=b; 13 | } 14 | }; 15 | 16 | class Rectangle: public Polygon { 17 | public: 18 | int area () 19 | { 20 | return width * height; 21 | } 22 | }; 23 | 24 | class Triangle: public Polygon { 25 | public: 26 | int area () 27 | { 28 | return width * height / 2; 29 | } 30 | }; 31 | 32 | int main () { 33 | Rectangle rect; 34 | Triangle trgl; 35 | rect.set_values (11,5); 36 | trgl.set_values (2,2); 37 | cout << rect.area() < 3 | using namespace std; 4 | class Account//This is base class 5 | { 6 | public: 7 | void display() 8 | { 9 | float salary=10000; 10 | cout<<"Salary:"< 2 | using namespace std; 3 | class A 4 | { 5 | int x=20; 6 | 7 | public: 8 | A change(A obja) 9 | { 10 | 11 | x=30; 12 | return obja; 13 | } 14 | void disp() 15 | { 16 | cout<x<x++; 24 | } 25 | void display() 26 | { 27 | cout<x< 3 | using namespace std; 4 | class Account 5 | { 6 | int acc_no=1111; 7 | public: 8 | void acc_details() 9 | { 10 | cout<<"Account"< 2 | using namespace std; 3 | class MyClass{ 4 | int x; 5 | mutable int y; 6 | public: 7 | MyClass(int x=0, int y=0){ 8 | this->x = x; 9 | this->y = y; 10 | } 11 | void setx(int x=0){ 12 | this->x = x; 13 | } 14 | void sety(int y=0) const { //member function is constant, but data will be changed 15 | this->y = y; 16 | } 17 | void display() const{ 18 | cout< 3 | 4 | // Define an outer class template named 'OuterClass' 5 | template 6 | class OuterClass { 7 | private: 8 | T outerData; // Data member of type T 9 | 10 | public: 11 | // Constructor for OuterClass 12 | OuterClass(T value) : outerData(value) {} 13 | 14 | // Nested class template inside OuterClass 15 | template 16 | class InnerClass { 17 | private: 18 | U innerData; // Data member of type U 19 | 20 | public: 21 | // Constructor for InnerClass 22 | InnerClass(U value) : innerData(value) {} 23 | 24 | // Method to set the value of innerData 25 | void setInnerData(U value) { 26 | innerData = value; 27 | } 28 | 29 | // Method to get the value of innerData 30 | U getInnerData() const { 31 | return innerData; 32 | } 33 | 34 | // Method to print the value of innerData 35 | void printInnerData() const { 36 | std::cout << "Inner Data: " << innerData << std::endl; 37 | } 38 | }; 39 | }; 40 | 41 | int main() { 42 | // Create an instance of OuterClass with int type 43 | OuterClass::InnerClass nestedInstance(30.5); 44 | nestedInstance.printInnerData(); // Output: Inner Data: 30.5 45 | 46 | return 0; 47 | } 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Nested-Class: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class University//enclosing class 4 | { 5 | protected: 6 | int id; 7 | string name; 8 | int rno; 9 | string fname; 10 | public: 11 | class Faculty 12 | { 13 | 14 | public: 15 | void input_details(University *uni) 16 | { 17 | cout<<"Enter name:"; 18 | cin>>uni->fname; 19 | cout<<"Enter Id"; 20 | cin>>uni->id; 21 | 22 | } 23 | void display_details(University *uni) 24 | { 25 | cout<id<<"\t"<name; 26 | } 27 | }; 28 | class Student 29 | { 30 | 31 | 32 | public: 33 | void input(University *uni) 34 | { 35 | cout<<"Enter Roll:"; 36 | cin>>uni->rno; 37 | cout<<"Enter Name:"; 38 | cin>>uni->name; 39 | } 40 | void display(University *uni) 41 | { 42 | cout<rno<<"\t"<name; 43 | } 44 | 45 | }; 46 | 47 | }; 48 | 49 | int main() 50 | { 51 | University::Faculty fact; 52 | University::Student stud; 53 | University uniobj; 54 | 55 | fact.input_details(&uniobj); 56 | fact.display_details(&uniobj); 57 | 58 | stud.input(&uniobj); 59 | stud.display(&uniobj); 60 | 61 | } 62 | -------------------------------------------------------------------------------- /New and arrow operator: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Employee { 7 | private: 8 | int id; 9 | char* name; 10 | 11 | public: 12 | Employee(int id, const char* name) { 13 | this->id = id; 14 | this->name = new char[strlen(name) ]; 15 | strcpy(this->name, name); 16 | } 17 | 18 | 19 | 20 | void print() { 21 | cout << "ID: " << id << ", Name: " << name << "\n"; 22 | } 23 | }; 24 | 25 | int main() { 26 | Employee* e = new Employee(1, "Nilesh Taware"); 27 | e->print(); 28 | 29 | e->print(); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Not using virtual function: -------------------------------------------------------------------------------- 1 | //when you are not using virtual function 2 | #include 3 | using namespace std; 4 | using namespace std; 5 | class A 6 | { 7 | int x=5; 8 | public: 9 | void display() 10 | { 11 | cout << "Value of x is : " << x<display(); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Overloading - operator: -------------------------------------------------------------------------------- 1 | // operator_overloading 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Base { 7 | 8 | int val; 9 | 10 | public: 11 | 12 | void get_Data() { 13 | 14 | cout << "Enter a value : "; 15 | 16 | cin >> val; 17 | 18 | } 19 | 20 | void print_Data() { 21 | 22 | cout << val << endl; 23 | 24 | } 25 | 26 | void operator -() { 27 | 28 | val = -val; 29 | 30 | } 31 | 32 | }; 33 | 34 | int main() { 35 | 36 | Base testobject; 37 | 38 | testobject.get_Data(); 39 | 40 | cout << "Before change the value is : "; 41 | 42 | testobject.print_Data(); 43 | 44 | -testobject; // It will call like testobject.operator -(); 45 | 46 | cout << "After change the value is : "; 47 | 48 | testobject.print_Data(); 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Parameterised constructor: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Dept 4 | { 5 | int dept_id; 6 | string dept_name; 7 | public: 8 | Dept(int,string); 9 | void display(); 10 | }; 11 | Dept:: Dept(int dummy_id,string dummuy_name) 12 | { 13 | dept_id=dummy_id; 14 | dept_name=dummuy_name; 15 | 16 | } 17 | void Dept::display() 18 | { 19 | cout< 3 | using namespace std; 4 | // Abstract class 5 | class Shape 6 | { 7 | public: 8 | virtual float calculateArea()=0; // pure virtual function. 9 | }; 10 | class Square : public Shape 11 | { 12 | float a; 13 | public: 14 | Square(float length) 15 | { 16 | a = length; 17 | } 18 | float calculateArea() 19 | { 20 | return a*a; 21 | } 22 | }; 23 | class Circle : public Shape 24 | { 25 | float radious; 26 | public: 27 | 28 | Circle(float x) 29 | { 30 | radious = x; 31 | } 32 | float calculateArea() 33 | { 34 | return 3.14*radious*radious ; 35 | } 36 | }; 37 | class Rectangle : public Shape 38 | { 39 | float l; 40 | float b; 41 | public: 42 | Rectangle(float x,float y) 43 | { 44 | l=x; 45 | b=y; 46 | } 47 | float calculateArea() 48 | { 49 | return l*b; 50 | } 51 | }; 52 | int main() 53 | { 54 | 55 | Shape *shape; 56 | 57 | Square sqrobj(3.4); 58 | Rectangle rectobj(5,6); 59 | Circle circleobj(7.8); 60 | 61 | shape =&sqrobj; 62 | 63 | int area1 =shape->calculateArea(); 64 | shape =&rectobj; 65 | 66 | int area2 =shape->calculateArea(); 67 | shape =&circleobj; 68 | 69 | int area3=shape->calculateArea(); 70 | 71 | cout <<"Area of the square is "< 3 | using namespace std; 4 | // Abstract class 5 | class Shape 6 | { 7 | public: 8 | virtual float calculateArea()=0; // pure virtual function. 9 | }; 10 | class Square : public Shape 11 | { 12 | float a; 13 | public: 14 | Square(float l) 15 | { 16 | a = l; 17 | } 18 | float calculateArea() 19 | { 20 | return a*a; 21 | } 22 | }; 23 | class Circle : public Shape 24 | { 25 | float r; 26 | public: 27 | 28 | Circle(float x) 29 | { 30 | r = x; 31 | } 32 | float calculateArea() 33 | { 34 | return 3.14*r*r ; 35 | } 36 | }; 37 | class Rectangle : public Shape 38 | { 39 | float l; 40 | float b; 41 | public: 42 | Rectangle(float x,float y) 43 | { 44 | l=x; 45 | b=y; 46 | } 47 | float calculateArea() 48 | { 49 | return l*b; 50 | } 51 | }; 52 | int main() 53 | { 54 | 55 | Shape *shape; 56 | 57 | Square sqrobj(3.4); 58 | Rectangle rectobj(5,6); 59 | Circle circleobj(7.8); 60 | 61 | shape =&sqrobj; 62 | 63 | int area1 =shape->calculateArea(); 64 | shape =&rectobj; 65 | 66 | int area2 =shape->calculateArea(); 67 | shape =&circleobj; 68 | 69 | int area3=shape->calculateArea(); 70 | 71 | cout <<"Area of the square is "< 3 | #include 4 | 5 | int main() { 6 | int integer = 42; 7 | float floatingPoint = 3.14f; 8 | char character = 'A'; 9 | std::string text = "Hello, World!"; 10 | 11 | // Using the insertion operator (<<) to output various types of data 12 | std::cout << "Integer: " << integer << std::endl; 13 | std::cout << "Floating-point number: " << floatingPoint << std::endl; 14 | std::cout << "Character: " << character << std::endl; 15 | std::cout << "String: " << text << std::endl; 16 | 17 | // Using the put() function to output a single character 18 | std::cout << "Using put() to output a single character: "; 19 | std::cout.put(character); 20 | std::cout << std::endl; 21 | 22 | return 0; 23 | } 24 | 25 | //Insert up 26 | -------------------------------------------------------------------------------- /Returning Object From Function: -------------------------------------------------------------------------------- 1 | //Program for returning object from function in c++ 2 | #include 3 | using namespace std; 4 | class Student { 5 | public: 6 | int studId; 7 | int studAge; 8 | string studName; 9 | 10 | // In this function we returning the student object from input function 11 | Student input(int tempstudId, int tempstudAge, string tempstudName) { 12 | Student obj; 13 | obj.studId = tempstudId; 14 | obj.studAge = tempstudAge; 15 | obj.studName = tempstudName; 16 | return obj; 17 | } 18 | // In this function we pass object as an argument to display function 19 | void display(Student obj) { 20 | 21 | cout << "Student Name: = " << obj.studName << endl; 22 | cout << "Student Id : = " << obj.studId << endl; 23 | cout << "Student Age : = " << obj.studAge << endl; 24 | } 25 | }; 26 | int main() { 27 | Student studobj; 28 | studobj = studobj.input (1212, 21, "Nilesh"); 29 | studobj.display(studobj); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Student Class With Public and Private: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Student 4 | { 5 | private: 6 | int rono; 7 | string name; 8 | int year_passing; 9 | 10 | public: 11 | 12 | void input_details(); 13 | 14 | void display_details(); 15 | 16 | }; 17 | 18 | void Student::input_details() 19 | { 20 | cout<<"Please enter student details:"; 21 | cout<<"Student Roll:"; 22 | cin>>rono; 23 | cout<<"Student Name:"; 24 | cin>>name; 25 | cout<<"YOP:"; 26 | cin>>year_passing; 27 | 28 | } 29 | void Student::display_details() 30 | { 31 | cout<<"----Student Details----"< 8 | using namespace std; 9 | class Student 10 | { 11 | int roll_no; 12 | string stud_name; 13 | double marks; 14 | public: 15 | void input_details(); 16 | 17 | void display_details(); 18 | 19 | 20 | }; 21 | void Student::input_details() 22 | { 23 | cout<<"Enter roll no:"<>roll_no; 25 | cout<<"Enter name:"; 26 | cin>>stud_name; 27 | cout<<"Enter marks:"; 28 | cin>>marks; 29 | 30 | } 31 | void Student::display_details(){ 32 | cout<<"---Student details---"< 8 | using namespace std; 9 | class Student 10 | { 11 | int roll_no; 12 | string stud_name; 13 | double marks; 14 | public: 15 | void input_details() 16 | { 17 | cout<<"Enter roll no:"<>roll_no; 19 | cout<<"Enter name:"; 20 | cin>>stud_name; 21 | cout<<"Enter marks:"; 22 | cin>>marks; 23 | 24 | 25 | } 26 | void display_details() 27 | { 28 | cout<<"---Student details---"< 3 | using namespace std; 4 | 5 | template 6 | class MyClass { 7 | private: 8 | T data; // Data member of type T 9 | 10 | public: 11 | // Constructor that takes a value of type T 12 | MyClass(T value) : data(value) {} 13 | 14 | // Method to set the value of data 15 | void setData(T value) { 16 | data = value; 17 | } 18 | 19 | // Method to get the value of data 20 | T getData() const { 21 | return data; 22 | } 23 | 24 | // Method to print the value of data 25 | void printData() const { 26 | cout << "Data: " << data < 3 | using namespace std; 4 | // Function template for adding two integers 5 | template 6 | T add(T a, T b) { 7 | return a + b; 8 | } 9 | 10 | // Overloaded function template for adding two doubles 11 | template <> 12 | double add(double a, double b) { 13 | return a + b; 14 | } 15 | 16 | // Overloaded function template for adding two strings 17 | template <> 18 | string add(string a,string b) { 19 | return a + b; 20 | } 21 | 22 | int main() { 23 | // Using the integer version of the add function 24 | int intResult = add(5, 3); 25 | cout << "Integer addition: " << intResult <(5.5, 3.3); 29 | std::cout << "Double addition: " << doubleResult <("Hello, ", "World!"); 33 | std::cout << "String concatenation: " << stringResult < 2 | using namespace std; 3 | 4 | class A { 5 | public: 6 | A() // Constructor 7 | { 8 | cout << "Constructor A\n"; 9 | } 10 | }; 11 | 12 | class B: public virtual A { 13 | }; 14 | 15 | class C: public virtual A { 16 | }; 17 | 18 | class D: public B, public C { 19 | }; 20 | 21 | int main() { 22 | D object; // Object creation of class D. 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /constructor overloading: -------------------------------------------------------------------------------- 1 | //constructor overloading with student class 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Student { 7 | private: 8 | string name; 9 | int age; 10 | string course; 11 | 12 | public: 13 | // Default constructor 14 | Student() { 15 | name = "Not Set"; 16 | age = 0; 17 | course = "Not Set"; 18 | } 19 | 20 | // Constructor with name and age 21 | Student(string n, int a) { 22 | name = n; 23 | age = a; 24 | course = "Not Set"; 25 | } 26 | 27 | // Constructor with name, age, and course 28 | Student(string n, int a, string c) { 29 | name = n; 30 | age = a; 31 | course = c; 32 | } 33 | 34 | // Getter methods 35 | string getName() { 36 | return name; 37 | } 38 | 39 | int getAge() { 40 | return age; 41 | } 42 | 43 | string getCourse() { 44 | return course; 45 | } 46 | // Display student details 47 | void displayStudentDetails() { 48 | cout << "Name: " << name <>): -------------------------------------------------------------------------------- 1 | //the extraction operator (>>) is used to read formatted input from the standard input stream (std::cin). 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | int integer; 7 | float floatingPoint; 8 | char character; 9 | string text; 10 | 11 | cout << "Enter an integer: "; 12 | cin >> integer; // Reads an integer 13 | cout << "You entered: " << integer <> floatingPoint; // Reads a floating-point number 17 | cout << "You entered: "<< floatingPoint <> character; // Reads a character 21 | cout << "You entered: "<< character < 2 | using namespace std; 3 | class Employee 4 | { 5 | int id; 6 | string name; 7 | double salary; 8 | public: 9 | void input_details() 10 | { 11 | cout<<"Enter id:"; 12 | cin>>id; 13 | cout<<"Enter name:"; 14 | cin>>name; 15 | cout<<"Enter salary:"; 16 | cin>>salary; 17 | } 18 | void display() 19 | { 20 | cout< 2 | using namespace std; 3 | class Test 4 | { 5 | int a, b, c; 6 | public: 7 | Test() 8 | { 9 | a = b = c = 0; 10 | } 11 | Test(int i, int j, int k) 12 | { 13 | a = i; 14 | b = j; 15 | c = k; 16 | } 17 | 18 | // use a reference to overload the ++ 19 | friend Test operator ++ (Test & op1); 20 | friend Test operator ++ (Test & op1, int not_used); 21 | 22 | void Display(); 23 | }; 24 | 25 | /* Overload prefix ++ using a friend function. 26 | This requires the use of a reference parameter. */ 27 | Test operator ++(Test & op1) 28 | { 29 | op1.a ++; 30 | op1.b ++; 31 | op1.c ++; 32 | return op1; 33 | } 34 | 35 | /* Overload postfix ++ using a friend function. 36 | This requires the use of a reference parameter. */ 37 | Test operator ++ (Test & op1, int not_used) 38 | { 39 | Test temp = op1; 40 | op1.a ++; 41 | op1.b ++; 42 | op1.c ++; 43 | return temp; 44 | } 45 | 46 | // Display a, b, c coordinates. 47 | void Test::Display() 48 | { 49 | cout << a << ", "; 50 | cout << b << ", "; 51 | cout << c << "\n"; 52 | } 53 | 54 | int main() 55 | { 56 | Test a (12, 22, 33); 57 | a.Display(); 58 | 59 | ++a; // prefix increment 60 | a.Display(); 61 | 62 | a++; // postfix increment 63 | a.Display(); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /this keyword in c++: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Student { 5 | private: 6 | string name; 7 | int id; 8 | string collegeName; 9 | 10 | public: 11 | Student(string n, int i, string c) 12 | { 13 | name=n; 14 | id=i; 15 | collegeName=c; 16 | } 17 | 18 | void display() { 19 | cout << "Name: " << this->name << ", ID: " << this->id << ", College: " << this->collegeName << endl; 20 | } 21 | 22 | void changeCollege(string newCollege) { 23 | this->collegeName = newCollege; 24 | } 25 | 26 | string getCollegeName() { 27 | return this->collegeName; 28 | } 29 | 30 | int getID() { 31 | return this->id; 32 | } 33 | 34 | string getName() { 35 | return this->name; 36 | } 37 | }; 38 | 39 | /// Main function with object and methods belongs to class 40 | int main() { 41 | 42 | Student s1("Shreyas", 32456, "JG College"); 43 | Student s2("Nilesh", 1232, "Mumbai University"); 44 | 45 | s1.display(); 46 | s2.display(); 47 | 48 | s1.changeCollege("DEF Institute"); 49 | 50 | cout <<"After changing college name for student 1:\n"; 51 | 52 | s1.display(); 53 | 54 | cout << "Student 1 college name: " << s1.getCollegeName() << endl; 55 | cout << "Student 1 ID: " << s1.getID() << endl; 56 | cout << "Student 1 name: " << s1.getName() << endl; 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /virtual Function In C++: -------------------------------------------------------------------------------- 1 | //Example using virtual function 2 | #include 3 | using namespace std; 4 | class A 5 | { 6 | public: 7 | virtual void display() 8 | { 9 | cout << "Base class is invoked"<display(); //Late Binding occurs 26 | } 27 | -------------------------------------------------------------------------------- /virtual function and method overriding: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Base class 5 | class Animal { 6 | public: 7 | virtual void sound() { 8 | cout << "The animal makes a sound" << endl; 9 | } 10 | }; 11 | 12 | // Derived class 13 | class Dog : public Animal { 14 | public: 15 | void sound() override { 16 | cout << "The dog barks" << endl; 17 | } 18 | }; 19 | 20 | // Derived class 21 | class Cat : public Animal { 22 | public: 23 | void sound() override { 24 | cout << "The cat meows" << endl; 25 | } 26 | }; 27 | 28 | int main() { 29 | Animal* animal1 = new Dog(); 30 | Animal* animal2 = new Cat(); 31 | 32 | animal1->sound(); // Outputs: The dog barks 33 | animal2->sound(); // Outputs: The cat meows 34 | 35 | delete animal1; 36 | delete animal2; 37 | 38 | return 0; 39 | } 40 | --------------------------------------------------------------------------------