Posts

Array of Objects & Passing Objects as Function Arguments in C++

  // #include<iostream> // using namespace std; // class Employee { //     int id; //     int salary; //     public: //     void getId(void){ //         cout<<"enter the id of the employee"; //         cin>>id; //     } //     void setId(void){ //         cout<<"the id of the employee is "<<id<<endl; //     } // }; // int main(){ //     Employee krishna, shalini, krisha, shana;     // Employee ks[4]; //an array which stores data of 4     // for (int i = 0; i<=4; i++)     // {     //     ks[4].getId();     //     ks[4].setId();     // }     //krishna.getId();     //krishna.setId();     //suppose i have a company named ks and it has 2000 employees and i need to store     //id and salary of ...

static data members and methods in c++

  #include <iostream> using namespace std ; class Employee {     int id;     static int count;         public:     void setdata ( void )     {         cout << "enter the id of the employee " ;         cin >> id;         count ++ ;     }         void getdata ( void )     {         cout << "the id of the employee is " << id << " and the employee number is " << count << endl;     }     static void getcount ( void ){         // cout<<id; this will through and error because id is not a static varibale         cout << "the value of count is " << count << endl;     }     //ye ek static member function hai jo dusre static varibales ya members   ...

memory allocation and using arrays in classes

Image
  //the statement that jab class likhte h to bilkul bi memory nhi khai jati and //objects bnane pe hi memory allocate hoti hai is not completlety true (partially true) /*jab hum class bnate hai to kuch memory (for example functions ki) sare functions ke liye same allocate ki jati hai otherwise har object ke liye alg memory allot hogi*/ #include <iostream> using namespace std ; class shop {     int itemId [ 100 ];     int itemPrice [ 100 ];     int counter;     public:     void initCounter ( void ) { counter = 0 ; }     void setPrice ( void );     void displayPrice ( void ); }; void shop :: setPrice ( void ){     cout << "enter id of your item no" << counter + 1 << endl;     cin >> itemId [counter];     cout << "enter price of your item" << endl;     cin >> itemPrice [counter];     counter ++ ; } void ...

oops recall, basic operations and nesting of member functions

  /*OOPS---> Classes and Objects C++--> initially called as C with classes by stroustroup Class--> extension of structures in C Structures had limitations     - members are public     - a function cannot be created inside structure     - no methods Classes --> Structures + more Classes --> can have methods and properties Classes --> can make few members as private and few as public Structues in c++ are typedef (mtlb typedef na lgakr bi structures ke varibales define kre to vo hojaenge) you can declare objects along with class declaration like this:     class Employee {           class definition           } krishna, shalini; harry.salary = 9 makes no sense if salary is private */ /************NESTING OF MEMBER FUNCTIONS******** */ #include <iostream> using namespace std ; // int main(){     //     binary b; //     b.read(); //   ...

OBJECT ORIENTED PROGRAMMING, classes public and private access modifiers (syntax)

/**************OBJECT ORIENTED PROGRAMMING (oops)************ */ /*why oops??? #c++ language was designed with the main intention of adding object oriented features to C language #as the size of program increases readability, maintaibility and bug free nature of the program decreases. #this was tha main problem with langage C #as a result the possiblity of not addressing the problem in an effective way increases #also, as data was almost neglected, data security was easily compromised #using classes solves this problem by modelling program as a real world scenario PROCEDURE ORIENTED PROGRAMMING (POP) (jo abi tak kr rahe the) #consists of writing a sequence of instructions for the computer to follow #main focus is one functions and not on the flow of data #functions can either use local or global data #data moves openly from function to function OBJECT ORIENTED PROGRAMMING #Works on the basis of CLASSES and OBJECTS #A class is a template to create objects #treats data as a critical elem...

Function Overloading in C++

  #include <iostream> using namespace std ; int sum ( int a , int b ){     cout << "by function1 is " ;     return a + b; } int sum ( int a , int b , int c ){     cout << "by function2 is " ;     return a + b + c; } int volume ( int a ){     return a * a * a; } int volume ( int r , int h ){     return 3.14 * r * r * h; } int volume ( int l , int b , int h ){     return l * b * h; } int main (){     cout << "the sum of 5 and 7 " << sum ( 5 , 7 ) << endl;     cout << "the sum of 5, 7 and 57 " << sum ( 5 , 7 , 57 ) << endl;     cout << "the volume of cube of side length 5 is " << volume ( 5 ) << endl;     cout << "the volume of cylinder of radius 5 and height 7 is " << volume ( 5 , 7 ) << endl;     cout << "the volume of cuboid of length 5, bredth 7...

recursions and recursive functins in c++

  //code for factorial #include <iostream> using namespace std ; int factorial ( int y ) {     if (y <= 1 ){         return 1 ;   //ye nhi btaenge to infinitely chalta rahega     }     return y * factorial (y - 1 ); } int fib ( int y ) {     if (y <= 2 ){         return 1 ;     }     return fib (y - 1 ) + fib (y - 2 ); } // fibonacci sequence int main (){     int x;     // cout<<"enter the number whose factorial u wanna find ";     // cin>>x;     // cout<<"the factorial of "<<x<<" is "<<factorial(x)<<endl;     cout << "enter the term u wanna find out in the fibonacci sequence" << endl;     cin >> x;     cout << "the term in fibonacci sequence at position " << x << " is " << fib (x);     retur...