LagAbuse.com

Unofficial Section => Programming => Topic started by: uCANseeMe on March 31, 2015, 18:07

Title: i need help for c++
Post by: uCANseeMe on March 31, 2015, 18:07
Ми треба програми од функции кои една програма со аргоменти една без аргументи една со преоптоварени функции и една со повик по референца. Treba mi programe u c++ od tema funkcije koja ce sodrzati argumenti jedna bez argumenta druga sa povik po referenca i treca sa preopotvareni funkcija :) english dont know sorry regrats tnx :)
Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 18:09
btw: need it until 21:00 gmt :)
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 18:12
Didn't quite understand what you want >.<

One function, with arguments, another one without arguments, and one of em with a call by reference ?

And the third one, overloading of functions or what?

Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 18:16
yes and one of them must have object :)
Title: Re: i need help for c++
Post by: ColdWorld on March 31, 2015, 18:23
dont know english but working in c++, how the fuck?
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 18:23
Something like this, if I got it right? I placed all functions in one program, but you can easily separate em if needed I guess.

#include <iostream>

using namespace std;
void fun1(){cout << "Hello" << endl;}
int fun2(int a){return a*a;}
int fun3(int &a){return a-2;}
int Saberi(int a,int b){return a+b;}
double Saberi(double a,double b){return a+b;}

int main()
{
fun1();

cout<< fun2(5) << endl;

int b=5;

cout << fun3(b)<< endl;

cout << Saberi(5,6) << "   " << Saberi(4.3,5.1) << endl;
}



btw, wrong part of forum, moderators move this to Programming I guess , not really tech support xd
Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 18:27
Преоптоварени функции

Понекогаш сакаме да креираме две или повеќе функции кои враќаат еден ист резултат (на пример, збир на броеви), но се извршуваат врз различни типови на податоци (цели броеви, реални броеви, еден цел и еден реален број, итн). C++ нуди механизам (т.н. преоптоварување на функции) преку кој неколку функции може да имаат едно исто име. За да се реализира ова, нормално, потребно е овие функции да се разликуваат по бројот на параметрите и/или по нивниот тип.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;
 
int zgolemi(int a, int b)
{
      return a+b;
}
 
int zgolemi(int a)
{
      return a+1;
}
 
double zgolemi(double a, double b)
{
      return a+b;
}
 
double zgolemi(double a)
{
      return a+1;
}
 
int main()
{
      cout << zgolemi(2, 3) << endl;        //se povikuva zgolemi(int a, int b)
      cout << zgolemi(5, 9) << endl;        //se povikuva zgolemi(int a, int b)
      cout << zgolemi(3) << endl;           //se povikuva zgolemi(int a)
       
      cout << zgolemi(3.0, 0.5) << endl;    //se povikuva zgolemi(double a, double b)
      cout << zgolemi(4.1, 2.9) << endl;    //se povikuva zgolemi(double a, double b)
      cout << zgolemi(2.0) << endl;         //se povikuva zgolemi(double a)
       
      return 0; someting like this
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 18:29
Something like this, if I got it right? I placed all functions in one program, but you can easily separate em if needed I guess.

#include <iostream>

using namespace std;
void fun1(){cout << "Hello" << endl;}
int fun2(int a){return a*a;}
int fun3(int &a){return a-2;}
int Saberi(int a,int b){return a+b;}
double Saberi(double a,double b){return a+b;}


int main()
{
fun1();

cout<< fun2(5) << endl;

int b=5;

cout << fun3(b)<< endl;

cout << Saberi(5,6) << "   " << Saberi(4.3,5.1) << endl;
}



btw, wrong part of forum, moderators move this to Programming I guess , not really tech support xd

Well, that's this part, add more of those if you want, it's same, just put other type of arguments.

btw, ZGOLEMI, rofl XD
Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 18:31
xD ma meni treba gotav program :D ovo je pre jednostavno ucim gimnaziju :D ....
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 18:44
Well, either explain me exactly what you need, or go study like other boys  :D
Title: Re: i need help for c++
Post by: maciekg9-slaveofBart on March 31, 2015, 18:49
WOW STALKER SUCH INFORMATICS SKILLS, B++ JAVA ALL KNOW WTF 8) 8) 8) 8) 8) 8) 8) 8)
Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 18:55
meni treba program eo sad cu ti dat primere ........ jedan sam ti vec dao eo ti jos #include <iostream>
using namespace std;

int soberi(int a, int b)
{
      cout << "2 ";
      int r = (a+b);
      return r;
}

//(x+x)*(y+y)
int mnozenje(int x, int y)
{
      cout << "4 ";
     
      int p = soberi(x, x);
      int v = soberi(y, y);
     
      return (p*v);
}

void ispechatiKraj()
{
      cout << "Kraj na programata." << endl;
}

int main()
{
      int a=4, b=4;
      cout << "1 ";
      int rezultat1 = soberi(3, 5);
      cout << "3 ";
     
      //presmetaj (a+a)*(b+b) + 1
      int rezultat2 = mnozenje(a, b) + 1;
      cout << "5 " << endl;
     
      //dosega, izlezot od programata e '1 2 3 4 2 2 5'
     
      cout << rezultat1 << endl;                         //pechati '8'
      cout << rezultat2 << endl;                         //pechati '65'
     
      cout << soberi(3, soberi(1, 2) + 1) << endl;       //pechati '7'
     
      //ispechati 'Kraj na programata.'
      ispechatiKraj();
     
      return 0;
#include <iostream>
using namespace std;

void prva(int a=7, int b=3, int c=5)
{
      cout << "a=" << a << ",b=" << b << ",c=" << c << endl;
}

int vtora(int a, int b, int c=3)
{
      return a+b+c;
}

int main()
{
      prva(1, 2, 3);                        //pechati 'a=1,b=2,c=3'
      prva(4, 5, 6);                        //pechati 'a=4,b=5,c=6'
     
      prva(0, 2);                           //pechati 'a=0,b=2,c=5'
      prva(3, 8);                           //pechati 'a=3,b=8,c=5'
     
      prva(0);                              //pechati 'a=0,b=3,c=5'
      prva(5);                              //pechati 'a=5,b=3,c=5'
     
      prva();                               //pechati 'a=7,b=3,c=5'
     
      cout << vtora(1, 2) << endl;          //pechati '6'
      cout << vtora(1, 2, 1) << endl;       //pechati '4'
     
      return 0;
}
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 19:06
Dunno, iskoristi nesto od ovoga sto ti stavih, ovaj sto ti ja napravih je komplikovaniji nego svi ovi tvoji iako je kraci xD

Sorry for non-eng :D
Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 19:09
a sto presmecuje tvoja programa ?
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 19:10
a sto presmecuje tvoja programa ?

presmecuje= ? xd
Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 19:14
sta radi :D
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 19:23
#include <iostream>
using namespace std;
void fun1(){cout << "Hello" << endl;}          // stampa hello
int fun2(int a){return a*a;}                         // kvadrira
int fun3(int &a){return a-2;}                       // oduzima 2 od vrijednosti proslijedjenoj po referenci
int Saberi(int a,int b){return a+b;}                // sabira 2 integera
double Saberi(double a,double b){return a+b;}           // sabira 2 doubla, preklopljena fja sa ovom iznad

int main()
{
fun1();

cout<< fun2(5) << endl;

int b=5;

cout << fun3(b)<< endl;

cout << Saberi(5,6) << "   " << Saberi(4.3,5.1) << endl;
}

Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 19:25
to ti je programa koja djete od 5 godina moze napisat xD
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 19:25
to ti je programa koja djete od 5 godina moze napisat xD

pa i ovi tvoji su isti takvi manje vise :D

reci sta ti treba tacno, otkud znam sta oces xD
Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 19:26
brate eo ti jos primere ili daj mi face tamo cemo se bolje razumeti ..
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 19:28
Napisi tacno sta ti treba, kakav program, pa cu napraviti, ovako ni sam ne znas sta oces xD


aaaaaaaaand again no english, I guess you'll have to delete all this posts soon :D
Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 19:31
jas znam sta hocu nego me ti ne razumes ... daj mi u box face tamo cemo se bolje razumeti od ovuda nema koristi :D
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 19:34
nope

Kad smislis sta ti treba napisi ovdje fino
Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 19:37
treba mi programa :D kao sto sam reko sa argumenta i bez i programa da nije bash jednostvna.2 treba mi programa sa vise funkcija unutra isto tako da nije bash jednostavna. i 3 treba mi programa koja sadrzi povik na po referenca isto tako da nije bash jednostavna. i jedna od ovih programa da sadrzi objects !!! eo ti
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 19:44
Class type objects? Ok, I'll make those 3 crappy programs soon
Title: Re: i need help for c++
Post by: Lagi on March 31, 2015, 19:46
!moved to right section. Good luck
Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 19:52
1 of the program must  have object another don't must :D
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 20:15
Quote
#include <iostream>

using namespace std;


void printArrayBackwards(int q[]){

for(int i=4;i>=0;i--)
    cout << q[ i ] << " ";

}

int main()
{


int q[5],a;

cout << "Unesi niz" << endl;
for(int i=0;i<5;i++){
cin >> a;
q[ i ] =a;
}
printArrayBackwards(q);


}

Quote
#include <iostream>

using namespace std;

class kompleks{

private: double Re,Im;

public:

    kompleks(int a,int b):Re(a),Im(b){};
    kompleks(){};
    kompleks Saberi(kompleks A);
    int getRe(){return Re;}
      int getIm(){return Im;}
};

kompleks kompleks::Saberi(kompleks A){
kompleks Temp(0,0);
Temp.Re=Re+A.Re;
Temp.Im=Im+A.Im;
return Temp;

}

int main()
{

kompleks A(5,6);
kompleks B(1,7);
kompleks C=A.Saberi(B);

cout << C.getRe() << " + i" << C.getIm() << endl;
}

Quote
#include <iostream>
#include <math.h>
using namespace std;

void Square(int &a){a=pow(a,2);}
void Square(double &a){a=pow(a,2);}

int main()
{

int a;
double b;

cout << "Unesi a i b" << endl;
cin >> a;
cin >> b;

Square(a);
Square(b);

cout << a << endl;

cout << b << endl;

}

Ako ti je nesto jednostavno, ili pretesko, jbg , snadji se sam, dosta od mene :)
Title: Re: i need help for c++
Post by: uCANseeMe on March 31, 2015, 20:44
sto ova zadnja kad onesem 3 i 5 kaze 9 i 24 :D treba 9 i 25 ? :D
Title: Re: i need help for c++
Post by: cacomonster on March 31, 2015, 21:29
Any Reason for using initializer lists in the constructor ?
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on March 31, 2015, 23:56
Any Reason for using initializer lists in the constructor ?

In this easy class, it is pretty much same as using regular stuff like

kompleks(int a,int b){Re=a; Im=b;};

But in programs where more classes are connected and some are derived from others, it can speed up process a bit, specially if there are many variables in class as far as I know.  I'm not exactly sure why, but compiler probably treats them like some special type of function, not like the regular call etc.

sto ova zadnja kad onesem 3 i 5 kaze 9 i 24 :D treba 9 i 25 ? :D

Neam pojma, bug, worked fine for me
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on April 01, 2015, 00:06
sto ova zadnja kad onesem 3 i 5 kaze 9 i 24 :D treba 9 i 25 ? :D

ah y, cause I made variable a an integer, function pow returns float, so it was 24.999smth = 24 as integer.

Just replace pow(a,2) with a*a
Title: Re: i need help for c++
Post by: castiele on April 01, 2015, 23:20
you just comlicating your life with c++,use python ofc;)
Title: Re: i need help for c++
Post by: cacomonster on April 01, 2015, 23:38
Well .. C++ is the strongest language .. ( just harder to use ) ..
Title: Re: i need help for c++
Post by: div.ide on April 02, 2015, 00:32
Well .. C++ is the strongest language .. ( just harder to use ) ..
To kill a fly, you don't need to use grenade. Besides, just because c++ (or hell, asm for that matter) can run faster, doesn't mean it will in hands of unexperienced dev. Moreover, unlike with python, javascript, php, java, and a bunch of others, you can't really make a webapp with c++ can you? :P (hint - web stuff is trending :P)

you just comlicating your life with c++,use python ofc;)
I don't think it's a matter of choice in this particular case.

sto ova zadnja kad onesem 3 i 5 kaze 9 i 24 :D treba 9 i 25 ? :D

ah y, cause I made variable a an integer, function pow returns float, so it was 24.999smth = 24 as integer.

Just replace pow(a,2) with a*a
You could also be aware about casting float to int, and round that either with round() function  (part of <math.h> library) or effectively do it manually by adding pow(a,2)+0.5 before cast to int (same result... in most cases) :P
Limited float precision can be a bitch.
Title: Re: i need help for c++
Post by: jeandarc on April 02, 2015, 11:36
Wise words lil divide, too bad your post was too long and i cba to read all of that. I like short and quality posts.

Thank you,
Regards,
Jean, love and peace.
Title: Re: i need help for c++
Post by: NiGhT^^StAlKeR on April 02, 2015, 22:10
You could also be aware about casting float to int, and round that either with round() function  (part of <math.h> library) or effectively do it manually by adding pow(a,2)+0.5 before cast to int (same result... in most cases) :P
Limited float precision can be a bitch.

Yea I know div.ide, the casting and adding 0.5 works also, and the round, and many more ways, but imo easiest was just to do as I said, replace it with a*a, so he doesn't need to think about those things so much :D

And about c++, it is usually used for harder stuff, not these easy tasks, but it was his school task so I decided to help, hopefully I managed to do it.

I prefer Matlab for my engineering problems which can be solved easier by using it.
C++ is too deep programming for me and my needs, but I can still do a lot in it, powerful stuff.
Title: Re: i need help for c++
Post by: uCANseeMe on April 03, 2015, 18:08
TY VERY MUCH <3
Title: Re: i need help for c++
Post by: jeandarc on April 04, 2015, 08:12
Np
Title: Re: i need help for c++
Post by: MyUberNick on May 08, 2015, 19:15
Any Reason for using initializer lists in the constructor ?

In this easy class, it is pretty much same as using regular stuff like

kompleks(int a,int b){Re=a; Im=b;};

But in programs where more classes are connected and some are derived from others, it can speed up process a bit, specially if there are many variables in class as far as I know.  I'm not exactly sure why, but compiler probably treats them like some special type of function, not like the regular call etc.

sto ova zadnja kad onesem 3 i 5 kaze 9 i 24 :D treba 9 i 25 ? :D

Neam pojma, bug, worked fine for me

Yes in theory constructor initialization lists are faster than assigning a value from the body of the constructor. It's because of how the class members are constructed.

If you don't use initialization list:
- all class members will be initialized with their default constructor
- and then in constructor body you will initialize them again and assign them to members

If you use initialization list:
- all class members will be initialized with their default constructor or whichever else constructor you chose in the initialization list so you are initializing members only once not twice

In practice compiler probably optimizes simple cases as in this thread.



you just comlicating your life with c++,use python ofc;)

+1, life is too short for C++, you need it if you need performance, if not don't complicate your life, learn Java,C#,some web language :).



Well .. C++ is the strongest language .. ( just harder to use ) ..
To kill a fly, you don't need to use grenade. Besides, just because c++ (or hell, asm for that matter) can run faster, doesn't mean it will in hands of unexperienced dev. Moreover, unlike with python, javascript, php, java, and a bunch of others, you can't really make a webapp with c++ can you? :P (hint - web stuff is trending :P)

+1. You can make a webapp, see you in few years :D.
Title: Re: i need help for c++
Post by: jeandarc on May 08, 2015, 19:44
Ivo stop rrying to be cool, you will never be cool