Jump to content

object oriented programming

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
sayer

sayer

    Newbie

  • Members
  • Pip
  • 4 posts
please,can you help me with some examples of programs in delphi or c.
thanks

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
C doesn't support object oriented programming. It has pointers, structures for that.

#3
cicce

cicce

    Newbie

  • Members
  • Pip
  • 7 posts
for example,but is c++ and it is not english language:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>

using namespace std;
class Zakaznik{private:
                       string meno,bydlisko,cislo;
                       int pocet;
               public:
               string Meno() {return meno;}
               string Bydlisko() {return bydlisko;}
               string Cislo() {return cislo;}
               int Pocet() {return pocet;}
               void Nastav_udaje(string m, string b, string c, int p);
               void Vypis();
               };
void Zakaznik::Nastav_udaje(string m, string b, string c, int p)
     {meno=m;
     bydlisko=b;
     cislo=c;
     pocet=p;
     }
void Zakaznik::Vypis()
     {
      cout<<"meno: "<<meno<<endl;
      cout<<"bydlisko: "<<bydlisko<<endl;
      cout<<"cislo: "<<cislo<<endl;
      cout<<"pocet: "<<pocet<<endl;                  
     }
int main(){
       
       int max=0,min=10000,maxp,minp;
       float priemer=0;
       string meno,bydlisko,cislo;
       int pocet,n;
       fstream subor("mobily.txt",fstream::in);
       subor>>n;
       subor.get();
       Zakaznik *pole=new Zakaznik[n];
       
for(int i=0;i<n;i++){
                     getline(subor,meno);
                     getline(subor,bydlisko);
                     getline(subor,cislo);
                     subor>>pocet;
                     subor.get();
                    
                     pole[i].Nastav_udaje(meno,bydlisko,cislo,pocet);
                    
                     pole[i].Vypis();
                     cout<<endl;
                     
                     if(pocet>max)
                     {max=pocet;
                     maxp=i;
                                  }
                    if(pocet<min)
                    {min=pocet;
                    minp=i;
                                 }
                    priemer+=pocet;
                     }
                       subor.close();
                       
                       
                     priemer=(priemer/n);
                  
       cout<<"Najviac pretelefonoval zakaznik: "<<endl;
       pole[maxp].Vypis();
        
       cout<<"Najmenej pretelefonoval zakaznik: "<<endl;
       pole[minp].Vypis();
       
       cout<<"priemer hovorov je: "<<priemer<<endl;       
       delete[] pole; 
       system("Pause");
       return 0;
       }

Edited by WingedPanther, 07 January 2010 - 01:21 PM.
add code tags (the # button)


#4
Hassan Seth

Hassan Seth

    Newbie

  • Members
  • PipPip
  • 10 posts
ADVICE!

keep code nicely indented!

#5
sayer

sayer

    Newbie

  • Members
  • Pip
  • 4 posts
can you send me more please?

#6
cicce

cicce

    Newbie

  • Members
  • Pip
  • 7 posts
yes,of course
about warning of good or bad file
fstream file("smth.txt",fstream::in);
if (!file.is_open())
{
cout << "bad "<< endl;
system("pause");
exit(1);
}
string Podnik::Povedz_nazov()
{ return nazov;
}
string Podnik::Povedz_krajinu()
{ return krajina;
}
int Podnik::Povedz_pocet()
{ return pocet;
}
float Podnik::Povedz_spotrebu()
{ return spotreba;
}

program about diferent alternative:

for(int i=0;i<n;i++){
getline(subor,nazov);
getline(subor,krajina);
subor>>pocet;
subor.get();
subor>>spotreba;
subor.get();
pole[i].Nastav_udaje(nazov,krajina,pocet,spotreba);
pole[i].Povedz_nazov();
pole[i].Povedz_krajinu();
pole[i].Povedz_pocet();
pole[i].Povedz_spotrebu();
pole[i].Vypis();
cout<<endl;
}
subor.close();
cout<<"Musia byt odstavene: "<<endl;
for(i=0;i<n;i++){
if ((pole[i].Povedz_spotrebu()>spotrebasvk) && (pole[i].Povedz_krajinu()=="Slovensko")
|| (pole[i].Povedz_spotrebu()>spotrebacz) && (pole[i].Povedz_krajinu()=="Cesko"))
{pole[i].Vypis();
suma+=pole[i].Povedz_pocet();
}
cout<<endl;
}
cout<<"out is: "<<suma<<endl;

#7
cicce

cicce

    Newbie

  • Members
  • Pip
  • 7 posts
and sth in english:

class tree
{
protected:
string name,colour;
int temp;
public:
tree(){};
tree(string n,string c,int t):name(n),colour©,temp(t){}
string Tell_name(){return name;}
string Tell_colour(){return colour;}
int Tell_temp(){return temp;}
void Tell_info();
};
class OvocnyStrom:public tree
{
private:
string uroda;
public:
OvocnyStrom(string u,tree t);
string Tell_urodu(){return uroda;}
void Tell_info();
};
void tree::Tell_info()
{
cout<<"Name: "<<name<<endl;
cout<<"Colour: "<<colour<<endl;
cout<<"Temp: "<<temp<<endl;
}
OvocnyStrom::OvocnyStrom(string u,tree t)
{
uroda=u;
name=t.Tell_name();
colour=t.Tell_colour();
temp=t.Tell_temp();
}
void OvocnyStrom::Tell_info()
{
cout<<"Name: "<<name<<endl;
cout<<"Colour: "<<colour<<endl;
cout<<"Temp: "<<temp<<endl;
cout<<"Uroda: "<<uroda<<endl;
}
int degree,i;

cout<<"Give temp: "<<endl;
cin>>degree;

tree t[2]={tree ("sth1","black",35),
tree ("sth2","white",25)};
OvocnyStrom os[2]={OvocnyStrom ("juny",tree ("strawbery","green",22)),
OvocnyStrom ("august",tree("blackberry","grey",27))};
for(i=0;i<2;i++) if ((degree<=(t[i].Tell_temp() + 5)) &&
(degree>=(t[i].Tell_temp() - 5)))
{
t[i].Tell_info();
}
for(i=0;i<2;i++) if ((degree<=(os[i].Tell_temp() + 5)) &&
(degree>=(os[i].Tell_temp() - 5)))
{
os[i].Tell_info();
}

#8
sayer

sayer

    Newbie

  • Members
  • Pip
  • 4 posts
thanks,if you have more,can send me

#9
James.H

James.H

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 866 posts
cicce put your code in code tags like you did before, this will prevent the forums BB code from interacting and also it's neater, good examples btw :)

#10
Hassan Seth

Hassan Seth

    Newbie

  • Members
  • PipPip
  • 10 posts
Some Template based OOP Structure!


#include<iostream>


using namespace std;



template <class T>

class SinglyLinkedList;


template <class T>

class SNode{

      

      private:

              T Data;

              SNode<T> *Next;

              friend class SinglyLinkedList<T>;

      public:

             SNode():Data(0),Next(NULL){}

             SNode(const T& d){Data=d;Next=NULL;}

             SNode(const T& d, const SNode<T> *ptr){Data=d;Next=ptr;}

             //~SNode(){Data=0;Next=NULL;}

             T getData(){return Data;}

             SNode<T>* getNext()const{return Next;}

      };


template <class T>

class SinglyLinkedList{

      

      private:

              SNode<T> *Head;

              unsigned int Size;

      public:

             SNode<T>* getHead(){return Head;}

             SinglyLinkedList():Head(NULL),Size(0){}

             SinglyLinkedList(SinglyLinkedList & rhs);

             void InsertAtLast(const T& d);

             void InsertAtFront(const T& d);

             void RemoveAtLast();

             T RemoveAtFront();

             void DeleteList();

             unsigned int getSize(){return Size;}

             SinglyLinkedList<T>& operator = (SinglyLinkedList<T>& rhs);

             void Print();

      };

      

template <class T>

SinglyLinkedList<T>& SinglyLinkedList<T>::operator = (SinglyLinkedList<T> & rhs)

{

    if (this->getSize()>0)

       this->DeleteList();

    if (rhs.getSize()!=0)

    {

         this->Size = rhs.getSize();

         SNode<T> *Temp = rhs.Head;

         SNode<T> *Temp2 = new SNode<T>(rhs.Head->getData());

         this->Head = Temp2;

         Temp = Temp->Next;

         while (Temp!=NULL)

         {

               Temp2->Next = new SNode<T> (Temp->getData());

               Temp2 = Temp2->Next;

               Temp = Temp->Next;

         }

         Temp2->Next = NULL;

         return (*this);

               

    }

}


      

template <class T>

void SinglyLinkedList<T>::DeleteList()

{

     if (Head!=NULL)

     {

        SNode<T> *Temp= Head;

        SNode<T> *Temp2;

        while (Temp!=NULL)

        {

              Temp2 = Temp;

              Temp = Temp->Next;

			  Temp2->Next = NULL;

              delete Temp2;        

        }

        Head = NULL;

     }

     Size = 0;

}



template <class T>

SinglyLinkedList<T>::SinglyLinkedList(SinglyLinkedList<T>& rhs):Head(NULL),Size(0)

{

    if (rhs.getSize()!=0)

    {

          Size = rhs.getSize();

          SNode<T> *Temp =rhs.Head;

          SNode<T> *TempO;

          TempO = new SNode<T> (rhs.Head->Data);

          Head = TempO;

          Temp = Temp->Next;

          while (Temp!=NULL)

          {

                TempO->Next = new SNode<T> (Temp->Data);

                Temp = Temp->Next;

                TempO = TempO->Next;

          }     

    }

}



template <class T>

void SinglyLinkedList<T>::RemoveAtLast()

{

     if (Head!=NULL)

     {

        SNode<T> *Temp = Head;

        

        while( (Temp->Next!=NULL) && (Temp->Next->Next!=NULL) )

             Temp = Temp->Next;

        if ( (Temp == Head) && (Size<2) )

        {

                 delete Temp;

                 Head = NULL;

        }

        else

        {

                 delete Temp->Next;

                 Temp->Next = NULL;

        }

     Size--;

     }

}



template <class T>

T SinglyLinkedList<T>::RemoveAtFront()

{

     T TData;

     if (Head!=NULL)

     {

        SNode<T> *Temp = Head->Next;

        Head->Next = NULL;

        TData = Head->Data;

        delete Head;

        Head = Temp;

        Size--;

     }

     return TData;

}

      

template <class T>

void SinglyLinkedList<T>::InsertAtLast(const T& d)

{

     SNode<T> *Temp=Head;

     

     if (Head==NULL)

     {

        Temp = new SNode<T>(d);

        Head = Temp;

     }

     else

     {

         while (Temp->Next!=NULL)

           Temp=Temp->Next;

         Temp->Next = new SNode<T>(d);

     }

     Size++;

}



template <class T>

void SinglyLinkedList<T>::InsertAtFront(const T& d)

{

     SNode<T> *Temp = new SNode<T>(d);

     //assert(Temp!=0);

     

     Temp->Next = Head;

     Head = Temp;

     Size++;

}



template<class T>

void SinglyLinkedList<T>::Print()

{

     SNode<T> *Temp=Head;

     cout<<endl<<"Elements Of Singly Linked List"<<endl;

     

     while (Temp!=NULL)

     {

           cout<<Temp->getData()<<endl;

           Temp = Temp->Next;

     }

}




#11
cicce

cicce

    Newbie

  • Members
  • Pip
  • 7 posts
some about vectors:

class vect
{
private:
float x,y,z;
public:
vect(){};
vect(float x,float y,float z);
void Tell();
vect Prod(float k,vect v);
float Prod(vect v,vect w);
};
vect::vect(float x,float y,float z)
{
this->x=x;
this->y=y;
this->z=z;
}
void vect::Tell()
{
cout<<"x: "<<x<<endl;
cout<<"y: "<<y<<endl;
cout<<"z: "<<z<<endl;
}
vect vect::Prod(float k,vect v)
{
vect z;
z.x=k*v.x;
z.y=k*v.y;
z.z=k*v.z;

return z;
}
float vect::Prod(vect v,vect w)
{
float k;
k=(v.x*w.x)+(v.y*w.y)+(v.z*w.z);
return k;
}


vect v(1.0,2.0,5.0);
vect suc1;
v.Tell();
suc1=v.Prod(2.0, v);
suc1.Tell();

vect w(3.0,1.0,1.0);
w.Tell();
float ss;
ss=v.Prod(v,w);
cout<<"scalar is> "<<ss<<endl;

#12
cicce

cicce

    Newbie

  • Members
  • Pip
  • 7 posts
and smth numerical:

class Person
{
private:
string name;
int age, weight, height;
public:
Person (string n, int a, int w, int h) {name=n;age=a;weight=w;height=h;};
int Age() {return age;};
int Weight() {return weight;};
int Height() {return height;};
void Tell();
};
void Person::Tell()
{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Weight: " << weight << endl;
cout << "Height: " << height << endl;
}
class Calc
{ private:
double height, age;
public:
float Num(int he, int we);
float Num(int he, int we, int ag);
};
float Calc::Num(int he, int we)
{
height = he;
height = (height / 100);
return (we / (height*height));
}
float Calc::Num(int he, int we, int ag)
{
height = he;
height = (height / 100);
age = ag;
age = (25 / age);
return ((we / (height*height)) * age);
}

float b;
Person sb1("p1 ", 22, 184, 80);
Person sb2("p2", 32, 105, 103);
Calc k;
b = k.Num(sb1.Height(), sb1.Weight());
sb1.Tell();
if (b > 25)
cout << "over: " << b << endl << endl << endl;
else
cout << "good: " << b << endl << endl << endl;
b = k.Num(sb2.Height(), sb2.Weight(), sb2.Age());
sb2.Tell();
if (b > 25)
cout << "over: " << b << endl << endl << endl;
else
cout << "good: " << b << endl << endl << endl;