• Sonuç bulunamadı

void main() { int a=5; cout<<kupbul(a)<<endl; system("pause"); } int kupbul(int x) { return x*x*x; }

N/A
N/A
Protected

Academic year: 2022

Share "void main() { int a=5; cout<<kupbul(a)<<endl; system("pause"); } int kupbul(int x) { return x*x*x; }"

Copied!
13
0
0

Yükleniyor.... (view fulltext now)

Tam metin

(1)

BTEP243 – Ara Sınavdan önceki tüm örnekler

DERS1: FONKSIYONLAR - DEĞER İLE ÇAĞIRMA / REFERENAS İLE ÇAĞIRMA / ADRES İLE ÇAĞIRMA Örnek 1:

#include<iostream>

using namespace std;

int kupbul(int);

void main() {

int a=5;

cout<<kupbul(a)<<endl;

system("pause");

}

int kupbul(int x) {

return x*x*x;

}

Örnek 2:

#include<iostream>

using namespace std;

void kupbul(int&);

void main() {

int a=5;

cout<<"KUPBUL fonksiyonunu cagirmadan once a degeri:"

<<a<<endl;

kupbul(a);

cout<<"KUPBUL fonksiyonunu cagirdiktan sonraki a degeri:"

<<a<<endl;

cout<<endl;

system("pause");

}

void kupbul(int&x) {

x= x*x*x;

}

Örnek 3:

#include<iostream>

using namespace std;

int kktopla(int&,int&);

void main() {

int a,b;

cout<<"Iki tam sayi giriniz:";

cin>>a>>b;

cout<<"a:"<<a<<" b:"<<b<<endl;

cout<<"a karesi + b kupu toplam:"<<kktopla(a,b)<<endl<<endl;

(2)

cout<<"a degerinin karesi:"<<a<<endl;

cout<<"b degerinin kupu:"<<b<<endl;

system("pause");

}

int kktopla(int& x,int& y) {

x=x*x;

y=y*y*y;

return x+y;

}

Örnek 4:

//deger ile cagirma

#include<iostream>

using namespace std;

void degistir(int*, int*);

void main() {

int a=5,b=7;

cout<<"Degisiklikten once a:"<<a<<" b:"<<b<<endl;

degistir(&a,&b);

cout<<"Degisiklikten sonra a:"<<a<<" b:"<<b<<endl;

system("pause");

}

void degistir(int *x, int *y) {

int temp;

temp=*x;

*x=*y;

*y=temp;

}

Örnek 5:

#include<iostream>

using namespace std;

int fonk(int =3);

void main() {

int a=5, b=7;

cout<<fonk(a)<<endl;

cout<<fonk(b)<<endl;

cout<<fonk(8)<<endl;

cout<<fonk()<<endl;

system("pause");

}

int fonk(int x) {

int k=10;

k=k+x;

return k;

}

(3)

Örnek 6:

#include<iostream>

using namespace std;

void fonk(int,int=20,int=30);

void main() {

int a=3,b=7,c=9;

fonk(a,c,b);

fonk(a,b);

fonk(1,2,a);

fonk(10);

system("pause");

}

void fonk(int x,int y,int z) {

cout<<x<<" "<<y<<" "<<z<<endl;

}

Örnek 7:

#include<iostream>

using namespace std;

int fonk(int=3);

void main() {

int a=3,b=7;

cout<<fonk(a)<<endl;

cout<<fonk(b)<<endl;

cout<<fonk(5)<<endl;

cout<<fonk()<<endl;

system("pause");

}

int fonk(int x) {

static int k=10;

k=k+x;

return k;

}

(4)

DERS2: FONKSIYONLAR - DEĞER İLE ÇAĞIRMA / REFERENAS İLE ÇAĞIRMA / ADRES İLE ÇAĞIRMA Örnek 1:

#include<iostream>

using namespace std;

void fonk(int,int=10);

int fonk(int,int &,int &);

void fonk();

void main() {

int a=3,b=5,c=7;

fonk();

cout<<a<<" "<<b<<" "<<c<<endl;

cout<<fonk(b,c,a)<<endl;

cout<<a<<" "<<b<<" "<<c<<endl;

fonk(c);

cout<<fonk(10,c,b)<<endl;

cout<<a<<" "<<b<<" "<<c<<endl;

fonk(c);

system("pause");

}

void fonk(int x,int y) {

static int t=2;

t+=x*y;

cout<<t<<endl;

}

int fonk(int x,int& y,int& z) {

x=y+z;

y++;

z--;

return x+y+z;

}

void fonk() {

cout<<"Bu bir denemedir."<<endl;

}

(5)

Örnek 2:

#include<iostream>

using namespace std;

int topla(int,int);

float topla(float,float);

double topla(double,double);

void main() {

int a=2,b=3;

float f=2.1, d=3.2;

double e=125.75, g=562.82;

cout<<topla(a,b)<<endl;

cout<<topla(f,d)<<endl;

cout<<topla(e,g)<<endl;

system("pause");

}

int topla(int x,int y) {

return x+y;

}

float topla(float x,float y) {

return x+y;

}

double topla(double x,double y) {

return x+y;

}

Örnek 3:

#include<iostream>

using namespace std;

int mutlakdeger(int);

float mutlakdeger(float);

double mutlakdeger(double);

void main() {

int a; float b; double c;

cout<<"Bir int sayi giriniz:";

cin>>a;

cout<<a<<" sayisinin mutlak degeri:"<<mutlakdeger(a)<<endl;

cout<<"Bir float sayi giriniz:";

cin>>b;

cout<<b<<" sayisinin mutlak degeri:"<<mutlakdeger(b)<<endl;

cout<<"Bir double sayi giriniz:";

cin>>c;

cout<<c<<" sayisinin mutlak degeri:"<<mutlakdeger(c)<<endl;

system("pause");

}

(6)

int mutlakdeger(int x) {

if(x>0) return x;

else

return x * (-1);

}

float mutlakdeger(float x) {

if(x>0) return x;

else

return x * (-1);

}

double mutlakdeger(double x) {

if(x>0) return x;

else

return x * (-1);

}

DERS 3 : SINIFLAR VE NESNELER Örnek 1:

//dikdortgen.h class dikdortgen{

private:

int uzunluk,genislik;

public:

//varsayilan yapici dikdortgen() {

/*uzunluk=0;

genislik=0;*/

cout<<"Uzunluk ve genislik degerlerini giriniz:";

cin>>uzunluk>>genislik;

}

//parametreli yapici dikdortgen(int u, int g) {

uzunluk=u;

genislik=g;

}

void setuzunluk(int u) {

uzunluk=u;

}

void setgenislik(int g) {

genislik=g;

}

int getuzunluk() {

(7)

return uzunluk;

}

int getgenislik() {

return genislik;

}

int getAlan() {

return genislik*uzunluk;

} };

//dikdortgen.cpp (main1)

#include<iostream>

using namespace std;

#include"dikdortgen.h"

void main() {

dikdortgen d1, d2(30,10);

//d1.dikdortgen();HATA cout<<d1.getuzunluk()<<" "

<<d1.getgenislik()<<endl;

cout<<d2.getuzunluk()<<" "

<<d2.getgenislik()<<endl;

//d1 nesnesinin uzunluk degerini 40 yapin d1.setuzunluk(40);

//d1 ve d2 nesnelerinin uzunluk, genislik ve //alanlarini ekrana yazdiriniz.

cout<<d1.getuzunluk()<<" "

<<d1.getgenislik()<<" ALAN:"

<<d1.getAlan()<<endl;

cout<<d2.getuzunluk()<<" "

<<d2.getgenislik()<<" ALAN:"

<<d2.getAlan()<<endl;

system("pause");

}

//dikdortgen.cpp (main2)

#include<iostream>

using namespace std;

#include"dikdortgen.h"

void main() {

dikdortgen ddizi1[5];

dikdortgen ddizi2[3]={dikdortgen(20,10),dikdortgen(30,20),dikdortgen(40,30)};

ddizi1[1].setuzunluk(60);

ddizi1[1].setgenislik(30);

cout<<"DDIZI1 NESNESININ DEGERLERI"<<endl;

for(int i=0;i<5;i++) {

cout<<"GENISLIK:"<<ddizi1[i].getuzunluk()

(8)

<<" UZUNLUK "<<ddizi1[i].getgenislik();

cout<<" ALAN:"<<ddizi1[i].getAlan()<<endl;

}

cout<<"DDIZI2 NESNESININ DEGERLERI"<<endl;

for(int i=0;i<3;i++) {

cout<<"GENISLIK:"<<ddizi2[i].getuzunluk() <<" UZUNLUK:"<<ddizi2[i].getgenislik();

cout<<" ALAN:"<<ddizi2[i].getAlan()<<endl;

}

system("pause");

}

DERS 4

Örnek 1: SINIFLAR VE NESNELER //musteri.h

class musteri { private:

double musteri_no;

string musteri_adi;

public:

musteri() {

musteri_no = 1;

musteri_adi = "A";

}

musteri(string ad, double y) { musteri_adi = ad;

musteri_no = y;

}

~musteri() {

cout << musteri_no<<endl;

}

string getMadi() {

return musteri_adi;

}

double getMno() {

return musteri_no;

}

void setMno(double x) { musteri_no = x;

}

void setMadi(string y){

musteri_adi = y;

} };

//musteri.cpp

#include <iostream>

(9)

#include <string>

using namespace std;

#include "musteri.h"

void main() { {

musteri musteri1;

musteri musteri2("hakki", 123456.0);

musteri1.setMadi("Ali");

musteri1.setMno(37);

cout << musteri1.getMadi() << " " << musteri1.getMno() << endl;

cout << musteri2.getMadi() << " " << musteri2.getMno() << endl;

system("pause");

}

system("pause");

}

DERS 5 – SINIFLAR – NESNELER Örnek 1:

//ogrenci.h class ogrenci{

private:

int ogrno;

string ograd;

static int ogrnosayac;

static int sayac;

public:

ogrenci() {

cout<<"Ogrenci adini giriniz:";

getline(cin,this->ograd);

this->ogrno=ogrnosayac;

ogrnosayac++; //her yeni ogrenci nesnesi icin artirma islemi sayac++; //nesne sayisini tutacak olan degisken

}

ogrenci(string ograd) {

this->ograd=ograd;

this->ogrno=ogrnosayac;

ogrnosayac++;

sayac++;

}

ogrenci(const ogrenci& eskiogr) {

this->ograd=eskiogr.ograd;

this->ogrno=ogrnosayac;

ogrnosayac++;

sayac++;

}

void yazdir() {

(10)

cout<<"Ogrenci no:"<<this->ogrno

<<" Ogrenci adi:"<<this->ograd<<endl;

}

static int getsayac() {

return sayac;

} };

int ogrenci::ogrnosayac=1000;

int ogrenci::sayac=0;

//ogrenci.cpp

#include<iostream>

#include<string>

using namespace std;

#include"ogrenci.h"

void main() {

ogrenci metin, biray("BIRAY");

ogrenci abdul(biray);

ogrenci ogrdizi[10]={ogrenci("A"), ogrenci("B"), ogrenci("C"), ogrenci("D"), ogrenci("E"), ogrenci("F"), ogrenci("G"), ogrenci("H"), ogrenci("J"), ogrenci("Z")};

metin.yazdir();

biray.yazdir();

abdul.yazdir();

for(int i=0; i<10; i++) ogrdizi[i].yazdir();

cout<<"Ogrenci sayisi:"<<ogrenci::getsayac()<<endl;

system("pause");

}

Örnek 2: STATIC – OTOMATIK – HARICI nesne örneği //abc.h

class abc { int sayi;

(11)

public:

abc () {

sayi = 0;

}

void sayiekle (int s) {

sayi += s;

}

int getsayi () {

return sayi;

}

~abc() {

cout<<this->sayi<<" degerine sahip olan nesne silinmistir:"

<<endl;

} };

//harici nesne abc haricinesne;

//abc.cpp (main1)

#include <iostream>

using namespace std;

#include"ornek.h"

void main( ) {

haricinesne.sayiekle(5);

// OTOMATİK NESNE abc otonesne;

otonesne.sayiekle (10);

// STATİK NESNE static abc statnesne;

statnesne.sayiekle (15);

cout << "\n \nOtomatik nesne değeri =" << otonesne.getsayi ();

cout << "\n \nStatik nesne değeri= "<< statnesne.getsayi () ;

cout << "\n\nHarici nesne değeri="<< haricinesne.getsayi ()<<endl;

system( "pause");

}

//abc.cpp (main2)

#include <iostream>

using namespace std;

#include"ornek.h"

void main( )

(12)

{

{

haricinesne.sayiekle(5);

for(int i=0; i <3 ;i ++) {

// OTOMATİK NESNE abc otonesne;

otonesne.sayiekle (10);

// STATİK NESNE static abc statnesne;

statnesne. sayiekle (15);

cout << "\n \nOtomatik nesne degeri =" << otonesne.getsayi ();

cout << "\n \nStatik nesne degeri= "<< statnesne.getsayi ()<<endl;

}

cout << "\n\nHarici nesne degeri="<< haricinesne.getsayi ()<<endl;

}

system( "pause");

}

DERS 6 – ŞABLON (TEMPLATE) FONKSIYONLAR

Örnek 1:

#include<iostream>

using namespace std;

template <class T>

T karebul(T a) {

return a*a;

}

void main() {

int x=5;

float y=2.1;

double z=25.25;

cout<<karebul(x)<<endl;

cout<<karebul(y)<<endl;

cout<<karebul(z)<<endl;

system("pause");

}

Örnek 2:

#include<iostream>

using namespace std;

template <class A>

void degistir(A &a, A &b) {

(13)

A c;

c=a;

a=b;

b=c;

}

void main() {

int x=2, y=3;

char c='C', d='D';

float f=1.2, g=2.3;

cout<<"Degistir fonksiyonunu cagirmadan onceki degerler:"<<endl;

cout<<"X:"<<x<<" Y:"<<y<<endl;

degistir(x,y);

cout<<"Degistir fonksiyonunu cagirdiktan sonraki degerler:"<<endl;

cout<<"X:"<<x<<" Y:"<<y<<endl;

cout<<"Degistir fonksiyonunu cagirmadan onceki degerler:"<<endl;

cout<<"C:"<<c<<" D:"<<d<<endl;

degistir(c,d);

cout<<"Degistir fonksiyonunu cagirdiktan sonraki degerler:"<<endl;

cout<<"C:"<<c<<" D:"<<d<<endl;

cout<<"Degistir fonksiyonunu cagirmadan onceki degerler:"<<endl;

cout<<"F:"<<f<<" G:"<<g<<endl;

degistir(f,g);

cout<<"Degistir fonksiyonunu cagirdiktan sonraki degerler:"<<endl;

cout<<"F:"<<f<<" G:"<<g<<endl;

system("pause");

}

Örnek 3:

#include<iostream>

using namespace std;

template <class A, class B, class C>

void topla(A a, B b, C c) {

cout<<a+b+c<<endl;

}

void main() {

int i=2, j=3;

float f=1.2, g=2.3;

double d=12.10, e=23.25;

cout<<i<<"+"<<f<<"+"<<d<<"=";

topla(i,f,d);

cout<<f<<"+"<<g<<"+"<<e<<"=";

topla(f,g,e);

cout<<d<<"+"<<j<<"+"<<g<<"=";

topla(d,j,g);

system("pause");

}

Referanslar

Benzer Belgeler

Yurtdışı Ajanda Tahm. Tüm bu öneriler İş Yatırım Araştırma Bölümü analistleri tarafından şirketlerin ileride elde edeceği tahmin edilen karları, nakit akımları

Lisans Lisans Ytiksek Lisans Doktora Conderen Kabul eden Ogrenci Sayrsr. De[i9im

Taban yarı¸capı 4, y¨ uksekli˘ gi 5 olan dik dairesel koni i¸cine ¸cizilebilen en b¨ uy¨ uk dik dairesel silindirin

Bloklar ‘{‘ ve ‘}’ ayraç işaretleri içinde belirtilen komutların bir araya gelmesi ile..

Aşağıdaki çarpma işlemi gerektiren problemleri çözünüz. 1) 36 sayısının 23 katı kaç eder? 6) Ahmet 24 sayfalık fotoğraf albümünün her sayfasına 6 fotoğraf koymuş. Her

Dik prizmaları tanır, temel elemanlarını belirler, inşa eder ve açınımını çizerX. Dik dairesel silindirin temel elemanlarını belirler, inşa eder ve

11. 52 yafl›ndaki bir baban›n üç çocu¤undan iki tanesi ikizdir. Di¤er çocuk, ikizlerden 5 yafl büyüktür. Bir baba ve iki çocu¤unun yafllar› toplam› 49 dur. Bir anne

[r]