Giter VIP home page Giter VIP logo

dac_c_modules's Introduction

DAC_C_modules

C++

d-155

stating with conditional operators and many more

msys2 compiler for c++

c++ installed in vs studio

27-09-2023

loop completed
for(;;);

D-147

4-10-23

function pointer

int (*fun)(int,int);

#include using namespace std; //function pointer int (*fun)(int,int); int sum(int,int); int sum(int,int,int); int main(){ int a=10,b=15,c=50; fun=sum; cout<<fun(a,b)<<" with fun pointer\n"; cout<<"\n"<<sum(a,b,c)<<" with direct 3 argument on calling sum\n"; fun=sum; cout<<"\n"<<fun(a,b,c)<<"\n"; return 0; } int sum(int a,int b){ return a+b; } int sum(int a,int b,int c){ return a+b+c; }
///////////////////////////////////////////////////////////////////////////

assignment 04-10-23

find sum... using function pointer
int (*fun)(int,int);
#include using namespace std; int (*fun)(int,int); int sum(int,int); int sub(int,int); int mul(int,int); int main(){ //using function pointer doing sum mul int a=40,b=30; fun=sum; cout<<"\n sum :="<<fun(a,b); fun=sub; cout<<"\n sub :="<<fun(a,b); fun=mul; cout<<"\n mul :="<<fun(a,b);

} int sum(int a,int b){ return a+b; } int sub(int a,int b){ return a-b; } int mul(int a,int b){ return a*b; }

///////////////////////////////////////////// using switch for choice on sum,sum
#include using namespace std; int (*fun)(int,int); int sum(int,int); int sub(int,int); int mul(int,int); int main(){ //using function pointer doing sum mul int a=40,b=30,x=0; cout<<"enter choice \n"; cin>>x; switch(x){ case 1: fun=sum; cout<<"\n sum :="<<fun(a,b); break; case 2: fun=sub; cout<<"\n sub :="<<fun(a,b); break; case 3: fun=mul; cout<<"\n mul :="<<fun(a,b); break; default: cout<<"enter right choice"; }

} int sum(int a,int b){ return a+b; } int sub(int a,int b){ return a-b; } int mul(int a,int b){ return a*b; } /////////////////////////////////////////////////////////////////////////////////// same upper question now by using reference variable

int sum(int &a,int &b);

for function syntax
for declaration on top === int sum(int &,int &);

/// #include using namespace std; int sum(int &,int &); int sub(int &,int &); int mul(int &,int &); int main(){ //using function pointer doing sum mul int a=40,b=30,x=0; cout<<"enter choice \n"; cin>>x; switch(x){ case 1: cout<<"\n sum :="<<sum(a,b); break; case 2: cout<<"\n sub :="<<sub(a,b); break; case 3: cout<<"\n mul :="<<mul(a,b); break; default: cout<<"enter right choice"; } //////////////////////////////////////////////////////

assigment on class constructor

1=> write a data class along with constructor
class std{ int a; public: std(int a){ this->a=a } }; int main(){ std a(10); } /////////////////////////////////////////////////////////

operator overloading

operator overloading is a amazing functionality provided by the c++ which is not available in java,
By the help of operator overloading we can add two objects also by using + operator, + operator is limited for arethmetic operations but by using operator overloading we can maximise the power of any operator. //// class A { statements; };

int main() { A a1, a2, a3;

a3 = a1 + a2; 

return 0; 

} in above code we are addin

operator overloading for adding two objects

#include using namespace std; class Date{ int dd,mm,yy; public: Date(){ } Date(int dd,int mm,int yy){ this->dd=dd; this->mm=mm; this->yy=yy; } void display(){ cout<<dd<<"-"<<mm<<"-"<<yy<<"\n"; } //operator overloading Date operator+(Date c){ Date date; date.dd=dd+c.dd; date.mm=mm+c.mm; date.yy=yy+c.yy; return date; }

~Date(){} }; int main(){ //date before update Date d(05,10,2023),e(1,1,1),f; d.display(); //date after update f=d+e; f.display(); }

operator * =

#include #include<string.h> using namespace std; class A{ int *x; public: A(){ x=new int; *x=9; } A(A const &v){ x=new int; *x=*v.x; } A(int v){ x=new int; *x=v; } void display(){ cout<<*x; } A operator=(A const v){ *x=*v.x; return *this; } A operator *(A const v){ A res; *res.x=(*x) * (v.x); return res; } ~A(){delete x;} }; int main(){ A a; A b(99),c(2),d; d=bc; d.display(); return 0; }

copy constructor for char

#include using namespace std; #include <string.h> class A{ char *c; public: A(){ c=new char[20]; strcpy(c,"\0"); } A(char *l){ c=new char[20]; strcpy(c,l); } A(A &h){ c=new char[20]; strcpy(c,h.c); } A operator=(A const &l){ strcpy(c,l.c); return *this; } void display(){ cout<<c;

}

~A(){delete []c;} };

int main(){ A p("ali"); A n(p); n.display(); return 0; }

using getter setter om date for updating class values (asss- on point)

#include using namespace std; class Point{ int x,y; public: Point(){ } Point(int x,int y){ this->x=x; this->y=y; } void setx(int x){ this->x=x; } void display(){ cout<<"("<<x<<","<<y<<")"<<"\n"; } ~Point(){} }; int main(){ Point p(10,12); p.display(); p.setx(99); p.display(); }

day-144

Integer class with copy constructor and operator overloading /// #include using namespace std; class Integer{ int a,b; public: Integer (){a=2;b=2;} Integer (int aa,int bb){ a=aa; b=bb; } // copy constructor Integer(Integer const &m){ a=m.a; b=m.b; } // operator overloading Integer operator+(Integer const &ob){ Integer res; res.a=a+ob.a; res.b=b+ob.b; return res; } //+= operator Integer operator+=(Integer const &ob){ Integer res; res.a=a+ob.a; res.b=b+ob.b; return res; } // bool == operator bool operator==(Integer const &ob){ if(a==ob.a && b==ob.b) return true;

} void display(){ cout<<a<<" " <<b<<"\n"; }

}; int main(){ Integer a,b(10,11),c(55,55),d(10,11); a.display(); b.display(); a=b; c=c+b; c.display(); c+=b; c.display(); if(c==d){cout<<"ok";} return 0; } ///

2D matrix using pointer

// creating 2-D array with pointer #include #include <Math.h> using namespace std;

class Matrix{ int row,col; int **mat; //default constructor public: Matrix(){ row=2; col=2; // creating address spaces in mat mat=new int *[row]; // creating colums value for(int i=0;i<row;i++) mat[i]=new int[col]; // filling array to 0 for(int i=0;i<row;i++){ for(int j=0;j<col;j++) mat[i][j]=0; } }

Matrix(int r,int c){ row=r; col=c; mat=new int *[row]; } // getting values of matrix from user void input(){ for(int i=0;i<row;i++){ cout<<"enter matrix value \n"; for(int j=0;j<col;j++) { //cout<<"enter matrix value \n"; cin>>mat[i][j]; } } }

//for displaying matrix void display(){ cout<<"our matrix will be \n"; for(int i=0;i<row;i++){ for(int j=0;j<col;j++) cout<<mat[i][j]<<" "; cout<<"\n"; } }

// for copying matrix Matrix (Matrix const &m){ row=m.row; col=m.col; mat=new int*[m.row]; for(int i=0;i<row;i++) mat[i]=new int[col];

for(int i=0;i<row;i++){ for(int j=0;j<col;j++) mat[i][j]=m.mat[i][j]; }

}

//destructor ~Matrix(){ for(int i=0;i<row;i++){ delete[] mat[i]; } delete [] mat; } }; int main(){ Matrix a; a.input(); a.display(); Matrix b(a); b.display(); return 0; } /////////////////////////////////////////

program for String including strcpy for getting string

include

include<string.h>

using namespace std; class StringX{ int len; char *str; //creating public type public: // creating constructor StringX(){ len=2; str=new char[len]; strcpy(str,"\0"); } //taking length StringX(int l){ len=l; str=new char[len]; } //talking string by base address of input string StringX(char *p){ len=strlen(p); str=new char[len]; strcpy(str,p); } // displaying string void display(){ cout<<"string is => "<<str<<"\n"; } // for copy constructor StringX(StringX const & s){ len=s.len; str=new char[len]; strcpy(str,s.str); }

//operator overloading StringX operator=(StringX const &o){

len=o.len; str=new char[len]; strcpy(str,o.str); return *this; } // for destructor ~StringX(){delete [] str; str=NULL;} };

int main(){ //StringX a(4); StringX a("hell"); a.display(); StringX m(a),p; m.display(); p=m;

p.display(); return 0; } ////////////////////////////////////////////////////////////////////////////////////////////

D-143 | 07-10-23

polymorphsim and inheritance

inheritance in c++ can achive when child class is derived from parent class. "child:public parent{}",this will create a child class.
A diamond problem will arise when the child class "D" is derived from calss "B" and "C" and the B C is also derived from "A", in this situation the methods in class A will also pass to B and C both on calling this function from class D the ambiguty will arrise. To sole this ambiguty we will use "virtual" keyword after ":" during extension of parent class in child class.

//
class A{ }; class B:virtual public A{ }; class C:virtual public A{ }; class D:public B,public C{ };

//


assignment-- --- #include using namespace std; // testing polymorphism //base class class base{ int a; public: base(){ a=10; }

void display(){ cout<<a<<"\n"; } };

//derived class class derive1:public virtual base{

}; class derive2:public virtual base{

}; class multipleDerived:public derive1,public derive2{

}; int main(){ base a; derive1 d1; derive2 d2; a.display(); d1.display(); d2.display(); // now calling the ambiguty class multipleDerived multiD; multiD.display();

return 0; }


polymorphism

The parent class or base class can hold the address of its derived class or child class.
A pure virtual function can be written to achive this.

pure virtual function

A vitual function is called pure virtual when it has no any methods written inside it ex= "virtual float A() =0;

To understand polymorphism better lets take example to find the area of any shape by declaring "shape" as base class and circle.squre as the derived class.

-- class area{

}; class square{

}; class circle{

};
///////////////
achiving polymorphism through ->
//
// plymorphism #include using namespace std; class Shape{ public: virtual float area() =0; };

class Square:public Shape{ int a,b; public: Square(){ a=1; b=1; } Square(int a,int b){ this->a=a; this->b=b; } float area(){ return a*b; }

}; class circle:public Shape{ int r; public: circle(){r=1;} circle(int r){ this->r=r; } float area(){ return 3.14rr; } };

int main(){
Square y(3,23);

// creating pointer of Shape class which will store the address of object
Shape *ptr;
ptr=&y;

// -> is used to print area from base class

cout<area() <<"\n"; circle m(9); ptr=&m;

cout<area()<<"\n";

return 0; }


//////
in above code there is three class is created the shape class is a parent class and two chiled clasess circle and square is derived from it,
when we want to use parent class which is "shape" to behave as multiple role we can achive it by storing address of child classe into parent class.
In c++ we can store address of chile in parent through->

{Shape *ptr;
ptr=&circle;}

this is a class pointer which can holed the address of chiled
The pure virtual method is declared inside the parent calss which help us to achive run time data binding.

run time data binding

Run time binding is achived in c++ by creating pointer of base class which holdes the address of the object of the deerived class.

The run time data binding means we can told complier to bind data not on complie time but on run time so that our method can behave virtually for my all child classes.

overriding

overriding is not a polymorphism
Overriding can be done when mehtod of base class is also written in child calss,then the child class method will be call due to overriding. (Overriding will happen when method in parent class has same method in child calss with same arguments in both of them).

D-141 | 09/10/23

assignment of creating salery for clerk,salesman and manager using Polymorphism

//testing polymorphism //testing polymorphism //testing polymorphism #include #include<string.h> using namespace std; class emp{ protected: char *name; float bsal;

public: // creating constructor for base class emp(){ name=new char[4]; strcpy(name,"\0"); bsal=1000; } emp(float bs){bsal=bs;} emp(float bs,char*naam){bsal=bs;name=new char[strlen(naam)+1];strcpy(name,naam);} emp(char *naam,float bs){ name=new char[strlen(naam)+1]; strcpy(name,naam); bsal=bs; } virtual float calNetSal()=0; virtual char *empname()=0; //using getter to get sal float getsal(){ return bsal; } //using getter to get name char *getname(){ return name; }

void display(){ cout<<name<<"\n"; cout<<bsal<<"\n"; } virtual ~emp(){ delete []name; }

}; class clerk:public emp{ int overtime; char *dept;

public: clerk(){overtime=0;dept=new char[20]; strcpy(dept,"\0");}

clerk(char *naam,float sal,int over,char dep):emp(sal,naam){ overtime=over; dept=new char[strlen(dep)+1]; strcpy(dept,dep); } float calNetSal(){ //cout<<"total salery of clerk : "; return (overtime1200)+getsal(); } char *empname(){ return getname(); } virtual ~clerk(){delete []dept; } };

// class for salesman

class salesman:public emp{ int saleamount; char *mgrname;

public: salesman(){saleamount=0;mgrname=new char[20];strcpy(mgrname,"\0");} salesman(char *naam,float sal,int amount,char *mgr):emp(sal,naam){ saleamount=amount; mgrname=new char[strlen(mgr)+1]; strcpy(mgrname,mgr); } void display(){ cout<<saleamount<<"\n"; cout<<mgrname<<"\n"; }

float calNetSal(){ //cout<<"salesman salery : "; return getsal()+(0.15*saleamount); }

char *empname(){ return getname(); }

virtual ~salesman(){ delete []mgrname; } };

class manager:public emp{ float bonus; char *dept;

public: manager(){ bonus=0; dept=new char[10]; strcpy(dept,"\0"); } manager(char *naam,float sal,float bon,char *dep):emp(sal,naam){ bonus=bon; dept=new char[strlen(dep)+1]; strcpy(dept,dep); } float calNetSal(){ //cout<<"sal of manager : "; return bonus+getsal(); }

char *empname(){ return getname(); }

virtual ~manager(){ delete []dept; } };

int main(){ emp *e[5]; e[0]=new clerk("Ankur",15000,20,"head"); e[1]=new salesman("Deepak",10000,500,"saleel"); e[2]=new manager("Kaushar",50000,1000,"principle"); //e[3]=new clerk(); //e[4]=new clerk();

for(int i=0;i<3;i++){ cout<< e[i]->calNetSal()<<"\n"; } cout<<"printing employee name whoes salary is greater than 15000"<<"\n"; for(int i=0;i<3;i++){ if(e[i]->calNetSal()>15000){ cout<<e[i]->empname()<<"\n"; } } cout<<"enter employee name to get its detail"<<"\n"; string naam; getline(cin,naam); for(int i=0;i<3;i++){ if(e[i]->empname()==naam){ cout<<e[i]->getname()<<"\n"; cout<<e[i]->getsal()<<"\n"; } }

for(int i=0;i<3;i++) delete e[i]; return 0; }

////////////////////////////////////////////////////////////////////////////////////////////////////

dac_c_modules's People

Contributors

mehdi9721 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.