Posts

Showing posts from November, 2024

File Handling Read and Write Binary Data

  #include <iostream> #include <fstream> #include <string> using namespace std ; struct MyStruct { int a ; //2 float b ; //4 double c ; //8 }obj ; int main() { obj.a = 10 ; obj.b = 30 ; obj.c = 4000 ; ofstream file( "abc.txt" , ios::binary | ios::out) ; if (file.is_open()) { file.write(reinterpret_cast<char*>(&obj) , sizeof(obj)) ; file.close() ; cout << "File Create Sucess." ; } //=============Read=========== ifstream file_read( "abc.txt" , ios::binary | ios::out) ; if (file_read.is_open()) { file_read.read(reinterpret_cast<char*>(&obj) , sizeof(obj)) ; file_read.close() ; cout << "File Read Sucess." ; } cout << "A=" << obj.a << endl ; cout << "C=" << obj.b << endl ; cout << "B=" << obj.c << endl ; ...

File Handing Read and Write Data and add new data

  #include<iostream> ; #include<fstream> #include<string> using namespace std ; int main() { string olddata ; //ios::in | ios::out | ios::trunc fstream file_obj( "sabir.xml" , ios:: in | ios::out | ios::trunc) ; string line ; if (file_obj.is_open()) { cout << "File is Open" ; file_obj << "Here a detailed overview and example of file handling in C++:" ; //====Read data======== file_obj.seekg( 0 ) ; getline(file_obj , line) ; cout << "Data=" << line ; //======Write data==== file_obj.seekp( 5 ) ; file_obj << "new data " ; //====Read data======== file_obj.seekg( 0 ) ; getline(file_obj , line) ; cout << "Data=" << line ; ////====Read data======= //file_obj.seekg(5); //getline(file_obj, olddata); file_obj. close () ; ...

File Handling Truc Mode

  #include <iostream> ; #include <fstream> #include <string> using namespace std; int main() { //Write ofstream file_obj("sabir.xyz"); file_obj << "abcd "; file_obj.close(); //============Read======== //fstream file_read("sabir.xyz",ios::trunc|ios::out|ios::in); //string data; //while (getline(file_read,data)) //{ // cout << data < <endl ; //} //file_read.close(); return 0; }

File Handling 2

  #include<iostream> ; #include<fstream> #include<string> using namespace std ; int main() { //Write //ofstream file_obj("sabir.xyz"); /*file_obj << "#$!!%&&()_{}[]|\\n \\t "; file_obj.close();*/ //============Read======== fstream file_read( "sabir.xyz" , ios::trunc|ios::out|ios:: in ) ; string data ; while (getline(file_read , data)) { cout << data <<endl ; } file_read.close() ; cout << "() \\ n \\ t couter " ; return 0 ; }

File Handing

  #include<iostream> ; #include<fstream> ; #include<string> using namespace std ; int main() { //ofstream file_obj("abc1.doc");//Create file and open ////fstream file_obj("abc1.txt",ios::in|ios::app); // //if (file_obj.is_open()) { // file_obj << " This is word Doc"; // file_obj.close(); //} //else //{ // cout << "file not found"; //} ifstream file_obj( "abc1.txt" ) ; string data ; if (file_obj.is_open()) { while (getline(file_obj , data)) { cout << data ; } } else { } return 0 ; }

Interface

  #include<iostream> ; using namespace std ; class ABC { public: virtual void SUB () = 0 ; virtual void SUB() = 0 ; } ; //interface and abstract method are same but one small changes // abstract class VS interface //cannot create object same //in abstract cls at list 1 only virtual fun interface class //virtual function define prototype or blue print of function //virtual function define //data member (int a;)and //member function class XYZ :public ABC { public: void SUB() override { } } ; int main () { //ABC obj; //B obj; // a=10 b=20,c=30; return 0 ; }

Encapsulation (it is a process of Data Hiding and protect your data and Get or Set Value using Getter and Setter Function )

  class TestClss { private : int a ; protected : int b ; public : int c ; void SetValue (int a ) { this -> a = a ; } int GetValue() { return a ; } } ; int main() { ////ABC obj; //B obj; // a=10 b=20,c=30; TestClss obj ; obj. SetValue ( 10 ) ; obj. c = 30 ; cout << "A=" << obj.GetValue()<< endl ; cout << "C=" << obj. c <<endl ; return 0 ; }

Data abstraction

  #include<iostream> ; using namespace std ; //abstract class //abstract class at list one abstarct method class ABC { public : //abstract method //pure virtual function virtual void Sum () = 0 ; } ; class B:ABC { public : void Sum() override { cout << "Call Sum" ; } } ; int main() { //ABC obj; B obj ; return 0 ; }

Friend Function

#include<iostream> ; using namespace std ; class Home1 { protected : int Pass = 1234 ; } ; class Home : public Home1 { friend void Tejas(Home) ; private : int Laptop = 1223 ; protected : int Pc = 90909 ; public : int Bike = 8888 ; void display() { cout << "Pass=" << Pass << endl ; } } ; void Tejas(Home obj2) { cout << "Laptop=" << obj2.Laptop << endl ; cout << "PC=" << obj2.Pc << endl ; cout << "Pass=" << obj2.Pass << endl ; obj2.display() ; } int main() { Home abc ; Tejas(abc) ; return 0 ; }

Problem 26 page (Multiple Inheritance ) solve by virtual

  #include<iostream> ; using namespace std ; class A { public : void Method_A() { cout << "Method_A" << endl ; } } ; class B:virtual public A { public : void Method_B() { cout << "Method_B" << endl ; } } ; class C:virtual public A { public : void Method_C() { cout << "Method_C" << endl ; } } ; class D : public B ,public C{ public : void Method_D() { cout << "Method_D" << endl ; } void display() { B::Method_A() ; Method_A() ; Method_B() ; Method_C() ; Method_D() ; } } ; int main() { D obj ; obj. Method_A () ; obj.Method_B() ; obj.Method_C() ; obj.Method_D() ; cout << "Display" << endl ; obj. display () ; return 0 ; }

Run Time Polymorphism ( Function override )

  #include<iostream> ; using namespace std ; class A { public : virtual void Sum() { cout << "Class A Sum" << endl ; } } ; class B : public A { public : void Sum() override { cout << "Class B Sum" << endl ; } } ; int main() { B obj ; obj.Sum() ; //Early Binding //A obj2; //obj2.Sum();//Early Binding A* p ; p = &obj ; //B p->Sum() ; // call A return 0 ; }

Binary Operator OverLoading

  #include<iostream> ; using namespace std ; class ABC { public : int a , b ; //10 20 ABC(int x , int y):a(x) , b(y) { } ABC operator +(ABC & abc) { return ABC(a + abc.a , b + abc.b) ; } } ; int main() { ABC a_obj( 10 , 20 ) , b_obj( 100 , 200 ) ; //c_obj a=110,b=220; //c=a + b; ABC c_obj = a_obj + b_obj ; cout << "A=" << c_obj.a << " B=" << c_obj.b ; return 0 ; }

1 Unary Operator Overloading

#include<iostream> ; using namespace std ; class ABC { public : int num ; /*ABC (){}*/ //Assign /*ABC(int no) { num = no; }*/ //init ABC(int no):num(no){} //======oprator Overloading======= ABC operator +() { //=======Custom Logic /*ABC abc(0); abc.num = num - 1;*/ return ABC(num- 1 ) ; } } ; int main() { ABC obj( 12 ) ; ABC result = +obj ; cout << "Result=" << result .num ; /*int num=0; num++; cout << "num=" << num << endl;*/ return 0 ; }  

Function Overloadin

  #include<iostream> ; using namespace std ; class ABC { public : void Sum() { cout << "1 Sum=" << 10 + 20 << endl ; } void Sum(int a , int b , int c) { cout << "2 Sum=" << a + b + c << endl ; } void Sum(float a , int b , int c) { cout << "3 Sum=" << a + b + c << endl ; } } ; int main() { float f = 12.0 ; ABC obj ; obj.Sum() ; obj.Sum( 11 , 34 , 45 ) ; obj.Sum(f , 34 , 45 ) ; return 0 ; }

1 Unary Operator Overloading

#include<iostream> ; using namespace std ; class UO { public : const int number ; //10 //Assigen UO(int num):number(num) { //number = num; } UO operator +() { //UO result(0); //result.number = number - 1;// //return result; return UO(number - 1 ) ; } UO Sabir() { return UO(number - 1 ) ; } } ; int main() { UO A_obj( 10 ) ; //10 //UO Output = +A_obj;//9 UO Output = A_obj.Sabir() ; //9 // cout << "Output=" << Output.number<<endl; cout << "Output=" << Output.number << endl ; cout << "A_obj=" << A_obj.number<<endl ; }  

Hierarchy

  #include <iostream> using namespace std ; class A { public : void Method_A() { cout << "Method_A" << endl ; } } ; class B: public A { public : void Method_B() { cout << "Method_B" << endl ; } } ; class C: public A { public : void Method_C() { cout << "Method_C" << endl ; } } ; class D:B , C { public : void Method_D() { cout << "Method_D" << endl ; } void Display() { B::Method_A() ; Method_B() ; Method_C() ; Method_D() ; } } ; int main() { D obj ; obj.Display() ; return 0 ; }

Access Modifier (Public, private ,protected)

  #include <iostream> using namespace std ; //Base Class class Sabir { public : char name[ 10 ] = "Sabir" ; //anyone call my name private : //Default int creditCard = 1234 ; //Information Access and use only Me protected : int mobilePass = 993 ; //Base Class and Child } ; //Sabir : Tejas is My Friend class Tejas: Sabir { public : void Display() { cout << "Name=" << name<<endl ; cout << "CardPin=" << creditCard << endl ; //Not Access cout << "mobilePass=" << mobilePass << endl ; //Not Access } } ; class Prem : public Tejas { public : void output() { cout << "Name=" << name << endl ; cout << "CardPin=" << creditCard << endl ; //Not Access cout << "mobilePass=" << mobilePass << endl ; //Not Access...

Multiple Inheritance

#include <iostream> using namespace std ; //MultiLevel class sabir { public : char name[ 10 ] = "Sabir" ; int CardPin = 3390 ; int mobilePass = 1200 ; } ; class Tejas { ///private public : int mobileNumber = 1233455 ; void UseData() { cout << "UseData" << endl ; } } ; class Prem : sabir , Tejas { public : void Display() { UseData() ; cout << "Name=" << name ; cout << "Mobile Number=" << mobileNumber ; } } ; int main() { Prem obj ; obj.Display() ; cout<< "===================" <<endl ; /*cout << "Name=" << obj.name << endl; cout << "Card=" <<obj.CardPin << endl; cout << "MobilePass=" <<obj.mobilePass << endl; */ /* OP obj; OP* obj2 = &obj; obj2->a = 10;*/ //ABC o...