Interactive Study Deck • Click or Press Space to Reveal Answers
.at().substr()try { // risky code } catch (const std::exception& e) { cout << e.what() << endl; }
e.what() to print the error message.vector<int> v = {1,2,3};
cout << v.at(5); // Index 5 doesn't exist
catch (???) {
cout << ???
}
.at() throws out_of_range when index is invalid.string str = "hello"; cout << str.at(10); catch (???) { cout << ??? }
out_of_range with .at().string s = "abc"; cout << s.substr(10, 2); // Pos 10 > len 3 catch (???) { cout << ??? }
.substr() throws length_error if pos > string length.vector<int> v; cout << v.at(0); // Vector is empty catch (???) { cout << ??? }
out_of_range.vector<int> v = {10,20};
cout << v.at(3); // Size is 2, index 3 invalid
catch (???) {
cout << ???
}
string s = "CS"; cout << s.at(5); catch (???) { cout << ??? }
if (top == -1) {
throw underflow_error("Stack underflow");
}
catch (???) {
cout << e.what() << endl;
}
if (top == 2) {
throw overflow_error("Stack overflow");
}
catch (???) {
cout << e.what() << endl;
}
virtual keyword enables dynamic binding.= 0 syntax makes a virtual function "pure".class Employee {
virtual void work() {...}
};
class Manager : public Employee {
void work() {...}
};
Employee* e = new Manager();
e->work();
vector<Employee*> staff; staff.push_back(new Employee()); staff.push_back(new Engineer()); staff.push_back(new Intern()); for (auto e : staff) { e->work(); }
class Employee {
virtual void work() = 0;
};
class Clerk : public Employee {
void work() {...}
};
int main() {
Employee e; // ERROR!
e.work();
}
class PartTime : public Employee { }; // Employee has pure virtual work() int main() { PartTime pt; // ERROR! pt.work(); }
PartTime is still abstract because it didn't implement work().class Employee {
virtual ~Employee() {...}
};
class Developer : public Employee {
~Developer() {...}
};
Employee* e = new Developer();
delete e;
class Employee {
void work() {...} // NOT virtual
};
class CEO : public Employee {
void work() {...}
};
Employee* e = new CEO();
e->work();
.at() → out_of_range.substr() → length_errore.what()= 0You've got this.