Çözüldü C++ hata -2

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

Primearc

Hectopat
Katılım
3 Ocak 2021
Mesajlar
1.489
Makaleler
3
Çözümler
13
Yer
Matrix
[CODE lang="cpp" title="C++"]#include <iostream>
#include <string>
#include <clocale>
using namespace std;

class Employee.
{
private:
int id;
string name;

public:
Employee(string name, int id) {
cout << "Constructor Cagrildi" << endl;
this->name = name;
this->id = id;

}
~Employee() {
cout << "Destructor Cagrildi" << endl;
}
getInfoName() {
return name;
}
getInfoId() {
return id;
}
};

int main()
{
setlocale(LC_ALL, "Turkish"); //Türkçe karakterler desteklenir.

int id;
string name;

cout << "ID:";
cin >> id;
endl;
cout << "Name: ";
cin >> name;

Employee* emp = new Employee();

emp->Employee(name, id);

cout << "İsim:" << emp->getInfoName << endl;
cout << "Numara:" << emp->getInfoId << endl;

delete emp;

return 0;
}[/CODE]

İşin içinden bir türlü çıkamadım nerelerde hata yapmışım. Bu hataların sebepleri neler ve çözümü nasıl olur?
 
Son düzenleyen: Moderatör:
Çözüm
Değişkenleri ilklendir.
Metotların dönüş türünü yaz.

Kodda ne yapmaya çalıştığını tam anlamadım. Java veya C#'ta bir şeyler bilip C++ serüvenine çıkmış gibisin.

Şimdi şu kod çalışıyor ve istediğin işi yapıyor olmalı.
[CODE title="C++"]#include <iostream>
#include <string>
#include <clocale>

using namespace std;

class Employee {
private:
int id;
string name;

public:
Employee(string name, int id) {
cout << "Constructor Cagrildi." << endl;
this->name = name;
this->id = id;
}

~Employee() {
cout << "Destructor Cagrildi." << endl;
}

string getInfoName() {
return name;
}

int getInfoId() {
return id;
}
};

int main() {
setlocale(LC_ALL, "Turkish"); //Türkçe karakterler desteklenir.

auto id{ 0 };
string name{ "" };

cout << "ID: ";
cin >> id;

cout << "Name: ";
cin >> name;

Employee emp(name, id);

cout << "İsim: " << emp.getInfoName() << endl;
cout << "Numara: " << emp.getInfoId() << endl;

return 0;
}[/CODE]

Bu da pointer'lı hali ama bu tip bir örnekte gerek yok.
[CODE title="C++"]#include <iostream>
#include <string>
#include <clocale>

using namespace std;

class Employee {
private:
int id;
string name;

public:
Employee(string name, int id) {
cout << "Constructor Cagrildi." << endl;
this->name = name;
this->id = id;
}

~Employee() {
cout << "Destructor Cagrildi." << endl;
}

string getInfoName() {
return name;
}

int getInfoId() {
return id;
}
};

int main() {
setlocale(LC_ALL, "Turkish"); //Türkçe karakterler desteklenir.

auto id{ 0 };
string name{ "" };

cout << "ID: ";
cin >> id;

cout << "Name: ";
cin >> name;

Employee* emp = new Employee(name, id);

cout << "İsim: " << emp->getInfoName() << endl;
cout << "Numara: " << emp->getInfoId() << endl;

delete emp; //destructor bunun sayesinde çağrılıyor

return 0;
}[/CODE]
Değişkenleri ilklendir.
Metotların dönüş türünü yaz.

Kodda ne yapmaya çalıştığını tam anlamadım. Java veya C#'ta bir şeyler bilip C++ serüvenine çıkmış gibisin.

Şimdi şu kod çalışıyor ve istediğin işi yapıyor olmalı.
[CODE title="C++"]#include <iostream>
#include <string>
#include <clocale>

using namespace std;

class Employee {
private:
int id;
string name;

public:
Employee(string name, int id) {
cout << "Constructor Cagrildi." << endl;
this->name = name;
this->id = id;
}

~Employee() {
cout << "Destructor Cagrildi." << endl;
}

string getInfoName() {
return name;
}

int getInfoId() {
return id;
}
};

int main() {
setlocale(LC_ALL, "Turkish"); //Türkçe karakterler desteklenir.

auto id{ 0 };
string name{ "" };

cout << "ID: ";
cin >> id;

cout << "Name: ";
cin >> name;

Employee emp(name, id);

cout << "İsim: " << emp.getInfoName() << endl;
cout << "Numara: " << emp.getInfoId() << endl;

return 0;
}[/CODE]

Bu da pointer'lı hali ama bu tip bir örnekte gerek yok.
[CODE title="C++"]#include <iostream>
#include <string>
#include <clocale>

using namespace std;

class Employee {
private:
int id;
string name;

public:
Employee(string name, int id) {
cout << "Constructor Cagrildi." << endl;
this->name = name;
this->id = id;
}

~Employee() {
cout << "Destructor Cagrildi." << endl;
}

string getInfoName() {
return name;
}

int getInfoId() {
return id;
}
};

int main() {
setlocale(LC_ALL, "Turkish"); //Türkçe karakterler desteklenir.

auto id{ 0 };
string name{ "" };

cout << "ID: ";
cin >> id;

cout << "Name: ";
cin >> name;

Employee* emp = new Employee(name, id);

cout << "İsim: " << emp->getInfoName() << endl;
cout << "Numara: " << emp->getInfoId() << endl;

delete emp; //destructor bunun sayesinde çağrılıyor

return 0;
}[/CODE]
 
Çözüm
En ufak hatada burada konu açmak yerine biraz mücadeleci olun. Kendi gelişiminiz için söylüyorum. Getter fonksiyonlarda tip ismini başına koymayı unutmuşsunuz.

[CODE lang="cpp" title="Düzgün kod, bir kaç hata daha vardı."]#include <iostream>
#include <string>
#include <clocale>
using namespace std;

class Employee
{
private:
int id;
string name;

public:
Employee(string name, int id) {
cout << "Constructor Cagrildi" << endl;
this->name = name;
this->id = id;

}
~Employee() {
cout << "Destructor Cagrildi" << endl;
}
string getInfoName() {
return name;
}
int getInfoId() {
return id;
}
};

int main()
{
setlocale(LC_ALL, "Turkish"); //Türkçe karakterler desteklenir.

int id;
string name;

cout << "ID:";
cin >> id;
cout << "Name: ";
cin >> name;

Employee* emp = new Employee("Abikardes47",20);


cout << "İsim:" << emp->getInfoName() << endl;
cout << "Numara:" << emp->getInfoId() << endl;

delete emp;

return 0;
}[/CODE]
 
Uyarı! Bu konu 6 yıl önce açıldı.
Muhtemelen daha fazla tartışma gerekli değildir ki bu durumda yeni bir konu başlatmayı öneririz. Eğer yine de cevabınızın gerekli olduğunu düşünüyorsanız buna rağmen cevap verebilirsiniz.

Bu konuyu görüntüleyen kullanıcılar

Technopat Haberler

Yeni konular

Geri
Yukarı