rockclimber
Početnik
- Poruka
- 13
Koja je razlika izmedju prototipa i definicije funkcije u C++ i koja je prednost koriscenja prototipa?
Donji video prikazuje kako da instalirate aplikaciju na početni ekran svog uređaja.
Napomena: This feature may not be available in some browsers.
#include<iostream>
int BaciPet()
{
return 5;
}
int main()
{
int n = BaciPet();
std::cout<<n;
return 0;
}
#include<iostream>
int BaciPet(); //prototip te funkcije
int main()
{
int n = BaciPet();
std::cout<<n;
return 0;
}
//ali tek ovde je definisana
int BaciPet()
{
return 5;
}
char* dupliraj_string(char*);
char* dupliraj_string(char* str)
{
// ...
}
#include <cstdlib>
#include <iostream>
int main(){
std::cout << sab(1,4);
std::system("PAUSE");
return 0;
}
int sab(int a,int b){
return a+b;
}
#include <cstdlib>
#include <iostream>
in sab(int a,int b);
int main(){
std::cout << sab(1,4);
std::system("PAUSE");
return 0;
}
int sab(int a,int b){
return a+b;
}
То, наравно, не ради. Али зато и ово ради:Kod:#include <cstdlib> #include <iostream> int main(){ std::cout << sab(1,4); std::system("PAUSE"); return 0; } int sab(int a,int b){ return a+b; }
#include <cstdlib>
#include <iostream>
int sab(int a,int b){
return a+b;
}
int main(){
std::cout << sab(1,4);
std::system("PAUSE");
return 0;
}
Најпростија примена им је једноставна прегледност у оквиру једног фајла. Неки *.c/*.cpp фајл се сматра читљивијим уколико на његовом почетку може да се види списак функција које садржи. А нанизани прототипи функција управо пружају ову форму.
#include <vector>
using namespace std;
class Parent;
class Child {
Parent* p;
public:
Child():
p(0)
{}
void setParent(Parent*p)
{ this->p = p; }
};
class Parent {
vector<Child*> children;
public:
Parent()
{}
void addChild(Child*c)
{
if(c)
{
c->setParent(this);
children.push_back(c);
}
}
};
#ifndef INC_PARENT
#define INC_PARENT
#include <vector>
class Child;
class Parent {
std::vector<Child*> children;
public:
Parent();
void addChild(Child*);
};
#endif
#include "Parent.h"
#include "Child.h"
Parent::Parent()
{}
void Parent::addChild(Child*c)
{
if(c)
{
c->setParent(this);
children.push_back(c);
}
}
#ifndef INC_CHILD
#define INC_CHILD
class Parent;
class Child {
Parent* p;
public:
Child();
void setParent(Parent*);
};
#endif
#include "Child.h"
Child::Child():
p(0)
{}
void Child::setParent(Parent*p)
{ this->p = p; }
#ifndef INC_MYHEAD
#define INC_MYHEAD
#include "Parent.h"
#include "Child.h"
#endif
#include "myhead.h"
int main()
{
Parent p;
Child a,b,c;
p.addChild(&a);
p.addChild(&b);
p.addChild(&c);
return 0;
}
Ako je bequick u pravu