I want to add two numbers using OOP way. I am a complete newbie in C++ and hence need your help.
#include <iostream>
#include <string>
using namespace std;
class RunwalsClass{
public: // public function
void setName(string x){
name = x;
}
string getName(){
return name;
};
private: // good programming practice to make it private
string name;
};
class MyClass{
public:
void setSaying(string y){
CoolSaying = y;
}
string getSaying(){
return CoolSaying;
}
private:
string CoolSaying;
};
class FavSitcom{
public:
void setSitcom(string z){
BreakingBad = z;
}
string getSitcom(){
return BreakingBad;
}
private:
string BreakingBad;
};
class AddClass{
public:
void setNumbers(int a, int b){
int answer = a + b;
}
int getAddition(){
return answer;
}
private:
int answer;
};
int main(){
RunwalsClass RunwalsObject;
RunwalsObject.setName("Sir Buckey Wallace");
cout << RunwalsObject.getName() << endl;
MyClass MyObject;
MyObject.setSaying("Preaching to the choir! \n");
cout << MyObject.getSaying();
FavSitcom MyNewObject;
MyNewObject.setSitcom("My favorite Sitcom is: Breaking Bad \n");
cout << MyNewObject.getSitcom();
AddClass NewObject;
NewObject.setNumbers("answer: \n");
cout << AddObject.getAddition();
return 0;
}
error: #include <iostream>
#include <string>
using namespace std;
class RunwalsClass{
public: // public function
void setName(string x){
name = x;
}
string getName(){
return name;
};
private: // good programming practice to make it private
string name;
};
class MyClass{
public:
void setSaying(string y){
CoolSaying = y;
}
string getSaying(){
return CoolSaying;
}
private:
string CoolSaying;
};
class FavSitcom{
public:
void setSitcom(string z){
BreakingBad = z;
}
string getSitcom(){
return BreakingBad;
}
private:
string BreakingBad;
};
class AddClass{
public:
void setNumbers(int a, int b){
int answer = a + b;
}
int getAddition(){
return answer;
}
private:
int answer;
};
int main(){
RunwalsClass RunwalsObject;
RunwalsObject.setName("Sir Buckey Wallace");
cout << RunwalsObject.getName() << endl;
MyClass MyObject;
MyObject.setSaying("Preaching to the choir! \n");
cout << MyObject.getSaying();
FavSitcom MyNewObject;
MyNewObject.setSitcom("My favorite Sitcom is: Breaking Bad \n");
cout << MyNewObject.getSitcom();
AddClass NewObject;
NewObject.setNumbers("answer: \n");
cout << AddObject.getAddition();
return 0;
}
Reported error:
error: no matching function for call to 'AddClass::setNumbers(const char [10])
note: candidate: void AddClass::setNumbers(int, int)
note: candidate expects 2 arguments, 1 provided.
In main, your are passing a string as parameter to your setNumbers method. This line is wrong in main:
NewObject.setNumbers("answer: \n");
Your setNumbers function neeeds 2 integers as it is declared. Try:
int a = 10;
int b = 5;
NewObject.setNumbers(a, b);
Good luck in your OOP learning journey!
EDIT:
Also, in your setNumbers function, you must not redeclare answer because this variable is a class member. Remove int, just use answer inside setNumbers.
Yep so your function setNumbers() expects two integer arguments provided, i.e. NewObject.setNumbers(5, 10); which would set the number to 15. You have provided a string literal "answer: \n" which is not the same and therefore will not compile.
Related
Compiling this code produces the error
error time constructor time::time(int,int,char) cannot be overloaded with time::time(int,int,char)
I'm trying to reduce overloaded constructors so I am trying to give default values in the constructor parameters. Is the line entry(int sno=5,time t{1,2,'p'}); in the constructor for the entry class valid? If a class contains a complex object of another class then can it be initialized this way?
#include<iostream>
using namespace std;
class time
{
int hours;
int mins;
char ap;
public:
time(int hours=0,int mins=0,char ap='n');
time(int a, int b, char c): hours{a},mins{b},ap{c}
{
}
void showtime()
{
cout<<"\nTime : "<<hours<<" "<<mins<<" "<<ap<<endl;
}
};
class entry{
int sno;
time t;
public:
entry(int sno=5,time t{1,2,'p'});
void showdata()
{
cout<<"\ne : "<<sno<<" : ";
t.showtime();
}
};
int main()
{
entry e;
e.showdata();
return 0;
}
Yes it's possible, this is just about syntax :
#include<iostream>
using namespace std;
class Time
{
int _hours;
int _mins;
char _ap;
public:
Time(int hours=0,int mins=0,char ap='n'): _hours(hours),_mins(mins),_ap(ap)
{};
void showtime()
{
cout<<"\nTime : "<< _hours << " " << _mins << " " << _ap << endl;
}
};
class entry{
int _sno;
Time _t;
public:
entry(int sno=5,Time t = Time(1,2,'p')):
_t(t), _sno(sno)
{};
void showdata()
{
cout<<"\ne : "<< _sno<<" : ";
_t.showtime();
}
};
int main()
{
entry e;
e.showdata();
Time t2(5,2,'a');
entry e2(3, t2);
e2.showdata();
return 0;
}
i just wanted to know if someone could help me about my code,i am a little confused to why it is not working like i want it to maybe i am misunderstanding something...The point of the program is to write a class with two functions to set and get a number but later on the main part of the code i have wanted it to print out a 2.52 number not just the number 2.Thank you if anyone helps :) .
#include <iostream>
#include<conio.h>
using namespace std;
class Class
{
public:
void Set(float x)
{
number = x;
}
int Get()
{
return number;
}
private:
float number;
};
int main()
{
Class object;
object.Set(2.52);
cout << "The number is: " << object.Get();
return 0;
}
First of all, you return an int from Get() so number will be converted in an int.
You should also make Get() const since it's will not change anything in the Class object when you call the function. Making it const makes it possible to pass instances of Class to functions taking a Class by const&:
#include <iostream>
class Class
{
public:
void Set(float x)
{
number = x;
}
float Get() const // returning float and added const
{
return number;
}
private:
float number;
};
void tester(const Class& obj) // a function taking a Class by const reference:
{
std::cout << "The number is: " << obj.Get() << '\n';
}
int main()
{
Class object;
object.Set(2.52);
tester(object);
}
Without the added const compilation would fail.
you can change get method type(float) like that
class Class
{
public:
void Set(float x)
{
number = x;
}
float Get()
{
return number;
}
private:
float number;
};
int main()
{
Class object;
object.Set(2.52);
cout << "The number is: " << object.Get();
return 0;
}
I cannot understand why this does not compile:
#include<iostream>
#include<string>
using namespace std;
class Product {
public:
virtual void print() = 0;
virtual void slog() = 0;
virtual void loft() = 0;
};
class Bike: public Product {
private:
string s;
public:
Bike(string x){
s = x;
}
void print() {
std::cout << "Bike";
}
int slog() {
return 4;
}
string loft() {
return s;
}
};
int main() {
string s("Hello");
Product *p = new Bike(s);
p->print(); //works fine
cout << p->slog();//works fine
cout << p->loft(); //error
return 0;
}
The above code results in error. Why can't I override string class.
I want to call loft() using the pointer p.
Is there any way to achieve this using pointer object to abstract class Product
Firstly, you need to include string #include <string>.
There's no problem with loft method, you have a problem with print method. Child class has a return type of string and base class has a return type of void, thus you're not really overriding the function. Compiler sees the declaration of void print() in base class and you can't do a cout on that.
Here's your code with few fixes and comments on them, it should work fine.
#include <iostream>
#include <string>
using namespace std;
class Product {
public:
virtual void print() = 0;
virtual int slog() = 0;
virtual string loft() = 0;
//added virtual destructor so you can use pointer to base class for child classes correctly
virtual ~Product() {};
};
class Bike: public Product {
string s;
public:
Bike(string x) {
s = x;
}
void print() {
cout << "Bike";
}
int slog() {
return 4;
}
string loft() {
return s;
}
};
int main() {
string s("Hello");
Product *p = new Bike(s);
p->print();
cout << p->slog();
cout << p->loft();
return 0;
}
Also, please try to format your code better next time, it makes it easier to read
I recently started playing around with c++, but for I don't understand what this means:
I get this error:
declaration is incompatible with "void
student_system::addStudent(<error-type> z)" (declared at line 31)
and the same goes for removeStudent and editStudent
I copied over the "function()" from another stackoverflow post, which seems fine and added the header with no issues, but my own "student" methods don't seem to work, I do not understand why,
I also tried adding the pointers rather than the variable but that didn't work either ( and by pointer I mean " student *x ").
#include "database.h"
#include <vector>
int main()
{
return 0;
}
class student_system
{
private:
list<student> studList;
public:
student_system();
void addStudent(student x);
void removeStudent(student y);
void editStudent(student z);
void findPos();
void function(int a, int b, vector<int> *p);
};
student_system::student_system()
{
//constructor
}
void student_system::addStudent(student x) // <------------- LINE 31
{
studList.push_back(x);
}
void student_system::removeStudent(student y)
{
/*studList.rem*/
}
void student_system::editStudent(student z)
{
/*get{ return value; }
set{ }*/
}
void student_system::findPos()
{
}
void student_system::function(int a, int b, vector<int> *p)
{
}
class student
{
private:
string name, surname, ID;
int sid;
public :
student::student(int sid, string n, string s, string id);
};
student::student(int sid, string n, string s, string id)
{
(*this).sid = sid;
(*this).name = n;
(*this).surname = s;
(*this).ID = id;
}
Put this bit of code
class student
{
private:
string name, surname, ID;
int sid;
public :
student::student(int sid, string n, string s, string id);
};
Just after the
#include <vector>
So that student_system and its definitions know about it
Add class student; above your student_system class definition.
Edit
Using forward declarations we can only declare methods in which we can use incomplete type not define. So, my first answer was wrong, but the following code will work.
int main()
{
return 0;
}
// Forward Declaration
class student;
// ================================
// Class student_system
// ================================
class student_system
{
private:
list<student> studList;
public:
student_system();
void addStudent(student x);
void removeStudent(student y);
void editStudent(student z);
void findPos();
void function(int a, int b, vector<int> *p);
};
// ================================
// Class student
// ================================
class student
{
private:
string name, surname, ID;
int sid;
public :
student(int sid, string n, string s, string id);
};
// ================================
// Definition of methods
// ================================
student::student(int sid, string n, string s, string id)
{
(*this).sid = sid;
(*this).name = n;
(*this).surname = s;
(*this).ID = id;
}
student_system::student_system()
{
//constructor
}
void student_system::addStudent(student x)
{
studList.push_back(x);
}
void student_system::removeStudent(student y)
{
/*studList.rem*/
}
void student_system::editStudent(student z)
{
/*get{ return value; }
set{ }*/
}
void student_system::findPos()
{
}
void student_system::function(int a, int b, vector<int> *p)
{
}
1) Please simplify a problem down to its basest elements before posting.
2) Please don't post code that relies on non-provided includes or implicit "using" statements or the like
3) "student" should be declared before student_system. The fact that you're not getting more errors surprises me, but maybe you're doing something unseen in database.h
I have a derived class named TimeWithDate inherited from Date class and Time class.
I try to use the member function by using ::.
like this:
int subtract(TimeWithDate& other_date){
return Date::subtract(other_date) + Time::subtract(other_date);
}
but I got this warning:
Error: a nonstatic member reference must be relative to a specific object.
Then I tried this way:
int subtract(TimeWithDate& other_date){
return *(Date*)this.subtract(other_date) + *(Time*)this.subtract(other_date);
}
and got this warning:
Error: 'this' may only be used inside a nonstatic member function.
What should I do?
whole code
#include<iostream>
using namespace std;
class Time
{
int hour, second, minute;
public:
Time();
Time(int h, int m, int s);
void set(int h, int m, int s);
void increment();
void display();
bool equal(Time &other_time);
bool less_than(Time &other_time);
int subtract(Time &another);
};
class Date
{
int year, month, day;
public:
Date();
Date(int y, int m, int d);
void increment();
bool equal(Date &another);
int subtract(Time &another);
};
class TimeWithDate : public Time, public Date
{
public:
bool compare(TimeWithDate&);
void increment();
int subtract(TimeWithDate&);
};
bool TimeWithDate::compare(TimeWithDate &other_date){
if (Date::equal(other_date) && Time::equal(other_date))
return true;
else return false;
}
void TimeWithDate::increment(){
Time::increment();
Time zero(0, 0, 0);
if (Time::equal(zero))
Date::increment();
}
int subtract(TimeWithDate& other_date){
return Date::subtract(other_date) + Time::subtract(other_date);
}
subtract() should be a member function of class TimeWithDate. It appears that you have it as a non-member/ static function. So, this pointer is no more available in that function.
You need parse your whole code, below works fine in my computer(VS2012).
#include <iostream>
using namespace std;
class Base1
{
public:
void print(const char *str){ cout << "base1 " << str << endl; }
};
class Base2
{
public:
void print(const char *str){ cout << "base2 " << str << endl; }
};
class Derived : public Base1, public Base2
{
public:
void print(const char *str);
};
void Derived::print(const char *str)
{
cout << "Derived " << str << endl;
Base1::print(str);
Base2::print(str);
}
int main()
{
Derived d;
d.print("hello");
return 0;
}